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
|
26 |
|
public const NAME = 'pattern'; |
16
|
|
|
|
17
|
26 |
|
public function name(): string |
18
|
|
|
{ |
19
|
|
|
return self::NAME; |
20
|
26 |
|
} |
21
|
|
|
|
22
|
26 |
|
public function testConditions(string $parameterName, $value, bool $itemType = false): string |
23
|
|
|
{ |
24
|
|
|
if ($itemType || !$this->getAttribute()->isArray()) { |
25
|
26 |
|
$valueToMatch = self::valueToRegularExpression($value); |
26
|
|
|
if (empty($valueToMatch)) { |
27
|
26 |
|
return ''; |
28
|
|
|
} |
29
|
|
|
|
30
|
26 |
|
$test = sprintf( |
31
|
|
|
($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', (string) $%1$s)', |
32
|
26 |
|
$parameterName, |
33
|
26 |
|
self::valueToRegularExpression($value) |
34
|
26 |
|
); |
35
|
26 |
|
} else { |
36
|
26 |
|
$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
|
|
|
} |
91
|
|
|
|