|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coduo\PHPMatcher\Matcher; |
| 4 | + |
| 5 | +use Coduo\PHPMatcher\Matcher\Pattern\TypePattern; |
| 6 | +use Coduo\PHPMatcher\Parser; |
| 7 | +use Coduo\PHPMatcher\Matcher\Pattern\RegexConverter; |
| 8 | +use Coduo\ToString\String; |
| 9 | + |
| 10 | +class TextMatcher extends Matcher |
| 11 | +{ |
| 12 | + const PATTERN_REGEXP = "/@[a-zA-Z\\.]+@(\\.[a-zA-Z0-9_]+\\([a-zA-Z0-9{},:@\\.\"'\\(\\)]*\\))*/"; |
| 13 | + |
| 14 | + const PATTERN_REGEXP_PLACEHOLDER_TEMPLATE = "__PLACEHOLDER%d__"; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var Parser |
| 18 | + */ |
| 19 | + private $parser; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var ValueMatcher |
| 23 | + */ |
| 24 | + private $matcher; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param ValueMatcher $matcher |
| 28 | + * @param Parser $parser |
| 29 | + */ |
| 30 | + public function __construct(ValueMatcher $matcher, Parser $parser) |
| 31 | + { |
| 32 | + $this->parser = $parser; |
| 33 | + $this->matcher = $matcher; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritDoc} |
| 38 | + */ |
| 39 | + public function match($value, $pattern) |
| 40 | + { |
| 41 | + if (!is_string($value)) { |
| 42 | + $this->error = sprintf("%s \"%s\" is not a valid string.", gettype($value), new String($value)); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + $patternRegex = $pattern; |
| 47 | + $patternsReplacedWithRegex = $this->replaceTypePatternsWithPlaceholders($patternRegex); |
| 48 | + $patternRegex = $this->prepareRegex($patternRegex); |
| 49 | + $patternRegex = $this->replacePlaceholderWithPatternRegexes($patternRegex, $patternsReplacedWithRegex); |
| 50 | + |
| 51 | + if (!preg_match($patternRegex, $value, $matchedValues)) { |
| 52 | + $this->error = sprintf("\"%s\" does not match \"%s\" pattern", $value, $pattern); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + array_shift($matchedValues); // remove matched string |
| 57 | + |
| 58 | + if (count($patternsReplacedWithRegex) !== count($matchedValues)) { |
| 59 | + $this->error = "Unexpected TextMatcher error."; |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + foreach ($patternsReplacedWithRegex as $index => $typePattern) { |
| 64 | + if (!$typePattern->matchExpanders($matchedValues[$index])) { |
| 65 | + $this->error = $typePattern->getError(); |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritDoc} |
| 75 | + */ |
| 76 | + public function canMatch($pattern) |
| 77 | + { |
| 78 | + if (!is_string($pattern)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Reaplce each type pattern (@string@.startsWith("lorem")) with placeholder, in order |
| 87 | + * to use preg_quote without destroying pattern & expanders. |
| 88 | + * |
| 89 | + * before replacement: "/users/@integer@.greaterThan(200)/active" |
| 90 | + * after replacement: "/users/__PLACEHOLDER0__/active" |
| 91 | + * |
| 92 | + * @param string $patternRegex |
| 93 | + * @return TypePattern[]|array |
| 94 | + */ |
| 95 | + private function replaceTypePatternsWithPlaceholders(&$patternRegex) |
| 96 | + { |
| 97 | + $patternsReplacedWithRegex = array(); |
| 98 | + preg_match_all(self::PATTERN_REGEXP, $patternRegex, $matches); |
| 99 | + |
| 100 | + foreach ($matches[0] as $index => $typePatternString) { |
| 101 | + $typePattern = $this->parser->parse($typePatternString); |
| 102 | + $patternsReplacedWithRegex[] = $typePattern; |
| 103 | + $patternRegex = str_replace( |
| 104 | + $typePatternString, |
| 105 | + sprintf(self::PATTERN_REGEXP_PLACEHOLDER_TEMPLATE, $index), |
| 106 | + $patternRegex |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return $patternsReplacedWithRegex; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /** |
| 115 | + * Replace placeholders with type pattern regular expressions |
| 116 | + * before replacement: "/users/__PLACEHOLDER0__/active" |
| 117 | + * after replacement: "/^\/users\/(\-?[0-9]*)\/active$/" |
| 118 | + * |
| 119 | + * @param $patternRegex |
| 120 | + * @return string |
| 121 | + * @throws \Coduo\PHPMatcher\Exception\UnknownTypeException |
| 122 | + */ |
| 123 | + private function replacePlaceholderWithPatternRegexes($patternRegex, array $patternsReplacedWithRegex) |
| 124 | + { |
| 125 | + $regexConverter = new RegexConverter(); |
| 126 | + foreach ($patternsReplacedWithRegex as $index => $typePattern) { |
| 127 | + $patternRegex = str_replace( |
| 128 | + sprintf(self::PATTERN_REGEXP_PLACEHOLDER_TEMPLATE, $index), |
| 129 | + $regexConverter->toRegex($typePattern), |
| 130 | + $patternRegex |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + return $patternRegex; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Prepare regular expression |
| 139 | + * |
| 140 | + * @param string $patternRegex |
| 141 | + * @return string |
| 142 | + */ |
| 143 | + private function prepareRegex($patternRegex) |
| 144 | + { |
| 145 | + return "/^" . preg_quote($patternRegex, '/') . "$/"; |
| 146 | + } |
| 147 | +} |
0 commit comments