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
|
34 |
|
public function name(): string |
18
|
|
|
{ |
19
|
34 |
|
return self::NAME; |
20
|
|
|
} |
21
|
|
|
|
22
|
34 |
|
public function testConditions(string $parameterName, $value, bool $itemType = false): string |
23
|
|
|
{ |
24
|
34 |
|
if ($itemType || !$this->getAttribute()->isArray()) { |
25
|
34 |
|
$test = sprintf( |
26
|
34 |
|
($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', $%1$s)', |
27
|
34 |
|
$parameterName, |
28
|
34 |
|
self::valueToRegularExpression($value) |
29
|
34 |
|
); |
30
|
|
|
} else { |
31
|
8 |
|
$this->addArrayValidationMethod($parameterName, $value); |
32
|
8 |
|
$test = sprintf( |
33
|
8 |
|
'\'\' !== (%s = self::%s($%s))', |
34
|
8 |
|
$this->getArrayErrorMessageVariableName($parameterName), |
35
|
8 |
|
$this->getValidationMethodName($parameterName), |
36
|
8 |
|
$parameterName |
37
|
8 |
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
34 |
|
return $test; |
41
|
|
|
} |
42
|
|
|
|
43
|
34 |
|
public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string |
44
|
|
|
{ |
45
|
34 |
|
if ($itemType || !$this->getAttribute()->isArray()) { |
46
|
34 |
|
$message = sprintf( |
47
|
34 |
|
'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))', |
48
|
34 |
|
self::valueToRegularExpression($value), |
49
|
34 |
|
$parameterName |
50
|
34 |
|
); |
51
|
|
|
} else { |
52
|
8 |
|
$message = $this->getArrayErrorMessageVariableName($parameterName); |
53
|
|
|
} |
54
|
|
|
|
55
|
34 |
|
return $message; |
56
|
|
|
} |
57
|
|
|
|
58
|
34 |
|
public static function valueToRegularExpression($value): string |
59
|
|
|
{ |
60
|
34 |
|
return implode( |
61
|
34 |
|
'|', |
62
|
34 |
|
array_map( |
63
|
34 |
|
static function ($value) { |
64
|
34 |
|
return addcslashes($value, '\'\\/'); |
65
|
34 |
|
}, |
66
|
34 |
|
array_map( |
67
|
34 |
|
static function ($value) { |
68
|
34 |
|
return empty($value) ? '^$' : $value; |
69
|
34 |
|
}, |
70
|
34 |
|
array_map('trim', is_array($value) ? $value : [$value]) |
71
|
34 |
|
) |
72
|
34 |
|
) |
73
|
34 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
protected function getArrayExceptionMessageOnTestFailure($value): string |
77
|
|
|
{ |
78
|
8 |
|
return sprintf( |
79
|
8 |
|
'Invalid value(s) %%s, please provide literals that are among the set of character sequences denoted by the regular expression /%s/\'', |
80
|
8 |
|
stripcslashes(self::valueToRegularExpression($value)), |
81
|
8 |
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|