1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
4
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\File\StructEnum; |
6
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class EnumerationRule |
10
|
|
|
* @link https://www.w3.org/TR/xmlschema-2/#rf-enumeration |
11
|
|
|
* Validation Rule: enumeration valid |
12
|
|
|
* A value in a ·value space· is facet-valid with respect to ·enumeration· if the value is one of the values specified in {value} |
13
|
|
|
*/ |
14
|
|
|
class EnumerationRule extends AbstractRule |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Struct |
19
|
|
|
*/ |
20
|
|
|
protected $model; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
111 |
|
public function name() |
26
|
|
|
{ |
27
|
111 |
|
return 'enumeration'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $parameterName |
32
|
|
|
* @param mixed $value |
33
|
|
|
* @param bool $itemType |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
111 |
|
public function testConditions($parameterName, $value, $itemType = false) |
37
|
|
|
{ |
38
|
111 |
|
$test = ''; |
39
|
111 |
|
if ($this->getRestrictionModel()) { |
40
|
111 |
|
$test = sprintf('!%s::%s($%s)', $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName); |
41
|
54 |
|
} |
42
|
111 |
|
return $test; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $parameterName |
47
|
|
|
* @param mixed $value |
48
|
|
|
* @param bool $itemType |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
111 |
|
public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false) |
52
|
|
|
{ |
53
|
111 |
|
$exceptionMessage = ''; |
54
|
111 |
|
if ($this->getRestrictionModel()) { |
55
|
111 |
|
$exceptionMessage = sprintf('sprintf(\'Invalid value(s) %%s, please use one of: %%s from enumeration class %2$s\', is_array($%1$s) ? implode(\', \', $%1$s) : var_export($%1$s, true), implode(\', \', %2$s::%3$s()))', $parameterName, $this->getRestrictionModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES); |
56
|
54 |
|
} |
57
|
111 |
|
return $exceptionMessage; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Struct|null |
62
|
|
|
*/ |
63
|
111 |
|
protected function getRestrictionModel() |
64
|
|
|
{ |
65
|
111 |
|
if (!$this->model) { |
66
|
111 |
|
$this->model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
67
|
54 |
|
} |
68
|
111 |
|
return $this->model; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|