Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       46 / 46  | 
               | 
       100.00%  | 
       5 / 5  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| PatternRule |         | 
       100.00%  | 
       46 / 46  | 
               | 
       100.00%  | 
       5 / 5  | 
       13 |         | 
       100.00%  | 
       1 / 1  | 
      
| name |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| testConditions |         | 
       100.00%  | 
       17 / 17  | 
               | 
       100.00%  | 
       1 / 1  | 
       5 | |||
| exceptionMessageOnTestFailure |         | 
       100.00%  | 
       8 / 8  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| valueToRegularExpression |         | 
       100.00%  | 
       16 / 16  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| getArrayExceptionMessageOnTestFailure |         | 
       100.00%  | 
       4 / 4  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| 1 | <?php | 
| 2 | |
| 3 | declare(strict_types=1); | 
| 4 | |
| 5 | namespace WsdlToPhp\PackageGenerator\File\Validation; | 
| 6 | |
| 7 | /** | 
| 8 | * @see https://www.w3.org/TR/xmlschema-2/#rf-pattern | 
| 9 | * Validation Rule: pattern valid | 
| 10 | * A literal in a ·lexical space· is facet-valid with respect to ·pattern· if: | 
| 11 | * - 1 the literal is among the set of character sequences denoted by the ·regular expression· specified in {value}. | 
| 12 | */ | 
| 13 | final class PatternRule extends AbstractRule | 
| 14 | { | 
| 15 | public const NAME = 'pattern'; | 
| 16 | |
| 17 | public function name(): string | 
| 18 | { | 
| 19 | return self::NAME; | 
| 20 | } | 
| 21 | |
| 22 | public function testConditions(string $parameterName, $value, bool $itemType = false): string | 
| 23 | { | 
| 24 | if ($itemType || !$this->getAttribute()->isArray()) { | 
| 25 | $valueToMatch = self::valueToRegularExpression($value); | 
| 26 | if (empty($valueToMatch)) { | 
| 27 | return ''; | 
| 28 | } | 
| 29 | |
| 30 | $test = sprintf( | 
| 31 | ($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', (string) $%1$s)', | 
| 32 | $parameterName, | 
| 33 | self::valueToRegularExpression($value) | 
| 34 | ); | 
| 35 | } else { | 
| 36 | $this->addArrayValidationMethod($parameterName, $value); | 
| 37 | $test = sprintf( | 
| 38 | '\'\' !== (%s = self::%s($%s))', | 
| 39 | $this->getArrayErrorMessageVariableName($parameterName), | 
| 40 | $this->getValidationMethodName($parameterName), | 
| 41 | $parameterName | 
| 42 | ); | 
| 43 | } | 
| 44 | |
| 45 | return $test; | 
| 46 | } | 
| 47 | |
| 48 | public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string | 
| 49 | { | 
| 50 | if ($itemType || !$this->getAttribute()->isArray()) { | 
| 51 | $message = sprintf( | 
| 52 | 'sprintf(\'Invalid value %%s, please provide a literal that is among the set of character sequences denoted by the regular expression /%s/\', var_export($%s, true))', | 
| 53 | self::valueToRegularExpression($value), | 
| 54 | $parameterName | 
| 55 | ); | 
| 56 | } else { | 
| 57 | $message = $this->getArrayErrorMessageVariableName($parameterName); | 
| 58 | } | 
| 59 | |
| 60 | return $message; | 
| 61 | } | 
| 62 | |
| 63 | public static function valueToRegularExpression($value): string | 
| 64 | { | 
| 65 | return implode( | 
| 66 | '|', | 
| 67 | array_map( | 
| 68 | static fn ($value) => addcslashes($value, '\'\\/'), | 
| 69 | array_map( | 
| 70 | static fn ($value) => empty($value) ? '^$' : $value, | 
| 71 | array_map( | 
| 72 | 'trim', | 
| 73 | array_filter( | 
| 74 | is_array($value) ? $value : [$value], | 
| 75 | static fn ($value) => !in_array($value, ['true', 'false', true, false], true) | 
| 76 | ) | 
| 77 | ) | 
| 78 | ) | 
| 79 | ) | 
| 80 | ); | 
| 81 | } | 
| 82 | |
| 83 | protected function getArrayExceptionMessageOnTestFailure($value): string | 
| 84 | { | 
| 85 | return sprintf( | 
| 86 | 'Invalid value(s) %%s, please provide literals that are among the set of character sequences denoted by the regular expression /%s/\'', | 
| 87 | stripcslashes(self::valueToRegularExpression($value)), | 
| 88 | ); | 
| 89 | } | 
| 90 | } |