match-column.php
6 years ago
match-context.php
6 years ago
match.php
6 years ago
preset.php
5 years ago
replace.php
6 years ago
result.php
6 years ago
search-flags.php
5 years ago
search.php
5 years ago
source-flags.php
5 years ago
source-manager.php
5 years ago
source.php
5 years ago
totals.php
6 years ago
match.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex; |
| 4 | |
| 5 | use SearchRegex\Match_Context; |
| 6 | |
| 7 | /** |
| 8 | * Represents a single match |
| 9 | */ |
| 10 | class Match { |
| 11 | /** |
| 12 | * Position ID |
| 13 | * |
| 14 | * @var Int |
| 15 | **/ |
| 16 | private $pos_id; |
| 17 | |
| 18 | /** |
| 19 | * Matched string |
| 20 | * |
| 21 | * @var String |
| 22 | **/ |
| 23 | private $match; |
| 24 | |
| 25 | /** |
| 26 | * Context offset |
| 27 | * |
| 28 | * @var Int |
| 29 | **/ |
| 30 | private $context_offset = 0; |
| 31 | |
| 32 | /** |
| 33 | * Replacement |
| 34 | * |
| 35 | * @var String |
| 36 | **/ |
| 37 | private $replacement; |
| 38 | |
| 39 | /** |
| 40 | * Array of captured data |
| 41 | * |
| 42 | * @var String[] |
| 43 | **/ |
| 44 | private $captures; |
| 45 | |
| 46 | /** |
| 47 | * Create a Match given the matched phrase, the match offset, and what the match is replaced with |
| 48 | * |
| 49 | * @param String $match The matched phrased. Typical the search phrase unless a regular expression search. |
| 50 | * @param int $match_offset The offset within the column. |
| 51 | * @param String $replacement The replaced value, if one is supplied. |
| 52 | */ |
| 53 | public function __construct( $match, $match_offset, $replacement ) { |
| 54 | $this->pos_id = intval( $match_offset, 10 ); |
| 55 | $this->match = $match; |
| 56 | $this->replacement = $replacement; |
| 57 | $this->captures = []; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add a regular expression capture value |
| 62 | * |
| 63 | * @param String $capture Captured value. |
| 64 | * @return void |
| 65 | */ |
| 66 | public function add_capture( $capture ) { |
| 67 | $this->captures[] = $capture; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get match position |
| 72 | * |
| 73 | * @return Int Position |
| 74 | */ |
| 75 | public function get_position() { |
| 76 | return $this->pos_id; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get matched text |
| 81 | * |
| 82 | * @return String Matched text |
| 83 | */ |
| 84 | public function get_matched_text() { |
| 85 | return $this->match; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Set the context offset - the offset within the context that this match belongs to |
| 90 | * |
| 91 | * @param Int $context_offset The context offset. |
| 92 | * @return void |
| 93 | */ |
| 94 | public function set_context( $context_offset ) { |
| 95 | $this->context_offset = $context_offset; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Convert this match to JSON |
| 100 | * |
| 101 | * @return Array JSON |
| 102 | */ |
| 103 | public function to_json() { |
| 104 | return [ |
| 105 | 'pos_id' => $this->pos_id, |
| 106 | 'context_offset' => $this->context_offset, |
| 107 | 'match' => $this->match, |
| 108 | 'replacement' => $this->replacement, |
| 109 | 'captures' => $this->captures, |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Encode a search as a regular expression |
| 115 | * |
| 116 | * @param String $search Search phrase. |
| 117 | * @param Search_Flags $flags Is this regular expression. |
| 118 | * @return String Encoded search phrase |
| 119 | */ |
| 120 | public static function get_pattern( $search, Search_Flags $flags ) { |
| 121 | $pattern = \preg_quote( $search, '@' ); |
| 122 | |
| 123 | if ( $flags->is_regex() ) { |
| 124 | $pattern = str_replace( '@', '\\@', $search ); |
| 125 | } |
| 126 | |
| 127 | $pattern = '@' . $pattern . '@'; |
| 128 | |
| 129 | if ( $flags->is_case_insensitive() ) { |
| 130 | $pattern .= 'i'; |
| 131 | } |
| 132 | |
| 133 | // UTF-8 support |
| 134 | return $pattern . 'u'; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get all matches for a search phrase on a column |
| 139 | * |
| 140 | * @param String $search The search phrase. |
| 141 | * @param Search_Flags $flags Any search flags. |
| 142 | * @param Array $replacements A matching set of replacements. |
| 143 | * @param String $column_value The content to match. |
| 144 | * @return Array Array of Match contexts |
| 145 | */ |
| 146 | public static function get_all( $search, Search_Flags $flags, array $replacements, $column_value ) { |
| 147 | $pattern = self::get_pattern( $search, $flags ); |
| 148 | $contexts = []; |
| 149 | |
| 150 | if ( \preg_match_all( $pattern, $column_value, $searches, PREG_OFFSET_CAPTURE ) > 0 ) { |
| 151 | $current_context = new Match_Context( 0 ); |
| 152 | $contexts[] = $current_context; |
| 153 | |
| 154 | // Go through each search match and create a Match |
| 155 | foreach ( $searches[0] as $match_pos => $match ) { |
| 156 | // Adjust for UTF8 strings |
| 157 | $pos = mb_strlen( substr( $column_value, 0, $match[1] ), 'utf-8' ); |
| 158 | |
| 159 | // Create a match |
| 160 | $match = new Match( $match[0], $pos, isset( $replacements[ $match_pos ] ) ? $replacements[ $match_pos ] : '' ); |
| 161 | |
| 162 | // Add any captures |
| 163 | foreach ( array_slice( $searches, 1 ) as $capture ) { |
| 164 | $match->add_capture( $capture[ $match_pos ][0] ); |
| 165 | } |
| 166 | |
| 167 | // Is the match within range of the current context |
| 168 | if ( ! $current_context->is_within_context( $match ) ) { |
| 169 | // No - create a new context |
| 170 | $current_context = new Match_Context( count( $contexts ) ); |
| 171 | $contexts[] = $current_context; |
| 172 | } |
| 173 | |
| 174 | // Add the match to the context |
| 175 | $current_context->add_match( $match, $column_value ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return $contexts; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Return the replacement at the matches position of the given text. |
| 184 | * |
| 185 | * @param String $text Text value. |
| 186 | * @return String The $text value, with the replacement inserted at the Match position |
| 187 | */ |
| 188 | public function replace_at_position( $text ) { |
| 189 | return mb_substr( $text, 0, $this->pos_id, 'UTF-8' ) . $this->replacement . mb_substr( $text, $this->pos_id + mb_strlen( $this->match, 'UTF-8' ), null, 'UTF-8' ); |
| 190 | } |
| 191 | } |
| 192 |