|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Maestroerror\EloquentRegex\Patterns; |
| 4 | + |
| 5 | +use Maestroerror\EloquentRegex\Contracts\PatternContract; |
| 6 | +use Maestroerror\EloquentRegex\Contracts\OptionContract; |
| 7 | +use Maestroerror\EloquentRegex\OptionsBuilder; |
| 8 | +use Maestroerror\EloquentRegex\Traits\Pattern; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class BasePattern |
| 12 | + * |
| 13 | + * Serves as a base class for regex patterns. Provides a basic structure and functionality |
| 14 | + * common to various types of regex patterns. |
| 15 | + */ |
| 16 | +class BasePattern implements PatternContract { |
| 17 | + |
| 18 | + use Pattern; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var array List of options applied to the pattern. |
| 22 | + */ |
| 23 | + protected array $options = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string The base regex pattern. |
| 27 | + */ |
| 28 | + protected string $pattern = "[a-z]"; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string Static property to define the pattern name. |
| 32 | + */ |
| 33 | + public static string $name = ""; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array Static property to define the pattern arguments one by one. |
| 37 | + */ |
| 38 | + public static array $args = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var array Static property to apply the default options for the pattern. |
| 42 | + */ |
| 43 | + public static array $defaultOptions = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string A string to hold expression flags for the regex pattern. |
| 47 | + */ |
| 48 | + protected string $expressionFlags = ""; |
| 49 | + |
| 50 | + /** |
| 51 | + * Retrieves the current regex pattern. |
| 52 | + * |
| 53 | + * @return string The current regex pattern. |
| 54 | + */ |
| 55 | + public function getPattern(): string { |
| 56 | + return $this->pattern; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Sets the options for this pattern. |
| 61 | + * |
| 62 | + * @param array $options Array of options to be applied to the pattern. |
| 63 | + */ |
| 64 | + public function setOptions(array $options) { |
| 65 | + $this->options = $options; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Adds an option to this pattern. |
| 70 | + * |
| 71 | + * @param OptionContract $option Option to be added. |
| 72 | + */ |
| 73 | + public function setOption(OptionContract $option) { |
| 74 | + $this->options[] = $option; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Validates an input string against the pattern and its options as exact match. |
| 80 | + * |
| 81 | + * @param string $input The input string to validate. |
| 82 | + * @return bool True if the input string validates against the pattern and options, false otherwise. |
| 83 | + */ |
| 84 | + public function validateInput(string $input): bool { |
| 85 | + // Get the main pattern |
| 86 | + $mainPattern = $this->getInputValidationPattern(); |
| 87 | + |
| 88 | + // First, check if the entire input matches the main pattern |
| 89 | + if (!preg_match($mainPattern, $input)) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + // Then, validate the input against each option |
| 94 | + return $this->validateOptions($input); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Validates that the input string contains matches for the pattern, filtered by options. |
| 99 | + * |
| 100 | + * @param string $input The input string to validate. |
| 101 | + * @return bool True if there are any matches for the pattern in the input, after applying options. |
| 102 | + */ |
| 103 | + public function validateMatches(string $input): bool { |
| 104 | + // Get the main pattern for matches |
| 105 | + $mainPattern = $this->getMatchesValidationPattern(); |
| 106 | + |
| 107 | + // Find all matches for the main pattern in the input |
| 108 | + if (preg_match_all($mainPattern, $input, $matches) == 0) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + // Filter these matches based on the options |
| 113 | + $filteredMatches = $this->filterByOptions($matches[0]); |
| 114 | + |
| 115 | + // Check if there are any matches left after filtering |
| 116 | + return count($filteredMatches) > 0; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Retrieves all matches of the pattern in the input string, filtered by options. |
| 121 | + * |
| 122 | + * @param string $input The input string to search for matches. |
| 123 | + * @return array An array of matches. |
| 124 | + */ |
| 125 | + public function getMatches(string $input, bool $returnGroups = false): ?array { |
| 126 | + $mainPattern = $this->getMatchesValidationPattern(); |
| 127 | + preg_match_all($mainPattern, $input, $matches); |
| 128 | + |
| 129 | + if (!$matches[0]) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + if ($returnGroups) { |
| 134 | + // Filter matches but keep indexes same |
| 135 | + $results = $this->filterByOptions($matches[0], false); |
| 136 | + // Unset matches and keep only groups |
| 137 | + unset($matches[0]); |
| 138 | + $groups = $matches; |
| 139 | + return [ |
| 140 | + "results" => $results, |
| 141 | + "groups" => $groups |
| 142 | + ]; |
| 143 | + } else { |
| 144 | + // Filter matches based on each option |
| 145 | + return $this->filterByOptions($matches[0]); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Filters an array of matches based on the options. |
| 151 | + * |
| 152 | + * @param array $allMatches Array of matches to be filtered. |
| 153 | + * @return array Filtered array of matches. |
| 154 | + */ |
| 155 | + protected function filterByOptions(array $allMatches, $fixArrayIndexes = true): array { |
| 156 | + // Use array_filter to keep only those matches that pass all options' validation |
| 157 | + $filtered = array_filter($allMatches, function($match) { |
| 158 | + return $this->validateOptions($match); |
| 159 | + }); |
| 160 | + |
| 161 | + if ($fixArrayIndexes) { |
| 162 | + return array_values($filtered); |
| 163 | + } else { |
| 164 | + return $filtered; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Validates an input string against all set options. |
| 170 | + * |
| 171 | + * @param string $input The input string to validate against the options. |
| 172 | + * @return bool True if the input string passes all options' validation, false otherwise. |
| 173 | + */ |
| 174 | + protected function validateOptions(string $input): bool { |
| 175 | + if (!empty($this->options)) { |
| 176 | + foreach ($this->options as $option) { |
| 177 | + if (!$option->validate($input)) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Default implementation of generating regex patterns for input validation |
| 187 | + * |
| 188 | + * @return string The regex pattern for validating the entire input. |
| 189 | + */ |
| 190 | + public function getInputValidationPattern(): string { |
| 191 | + return "/^{$this->pattern}$/" . $this->expressionFlags; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Default implementation of generating regex patterns for matches validation |
| 196 | + * |
| 197 | + * @return string The regex pattern for finding matches within the input. |
| 198 | + */ |
| 199 | + public function getMatchesValidationPattern(): string { |
| 200 | + return "/{$this->pattern}/" . $this->expressionFlags; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Processes an array of arguments and builds an options array. |
| 205 | + * |
| 206 | + * @param array $args Names of the arguments. |
| 207 | + * @param array $values Values of the arguments. |
| 208 | + * @return array An associative array of options. |
| 209 | + */ |
| 210 | + protected static function processArguments(array $args, array $values): array { |
| 211 | + $options = []; |
| 212 | + // Build options array based on condition |
| 213 | + for ($i=0; $i < count($args); $i++) { |
| 214 | + if (isset($values[$i])) { |
| 215 | + // If value is true (so not "", 0, null) |
| 216 | + if ($values[$i]) { |
| 217 | + $options[$args[$i]] = $values[$i]; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return $options; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Processes a callback function to configure options. |
| 227 | + * |
| 228 | + * @param callable $callback The callback function used for configuring options. |
| 229 | + * @return array An associative array of options set by the callback. |
| 230 | + */ |
| 231 | + protected static function processCallback(callable $callback): array { |
| 232 | + $optionsBuilder = new OptionsBuilder(); |
| 233 | + $callback($optionsBuilder); |
| 234 | + return $optionsBuilder->getOptions(); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Adds a regex expression flag to the pattern. |
| 239 | + * |
| 240 | + * @param string $flag The single-character flag to add to the regex pattern. |
| 241 | + */ |
| 242 | + public function addExpressionFlag(string $flag): void { |
| 243 | + if (strpos($this->expressionFlags, $flag) === false) { |
| 244 | + $this->expressionFlags .= $flag; |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments