|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter; |
|
6
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute; |
|
7
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
|
8
|
|
|
|
|
9
|
|
|
class ChoiceRule extends AbstractRule |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return string |
|
14
|
|
|
*/ |
|
15
|
24 |
|
public function name() |
|
16
|
|
|
{ |
|
17
|
24 |
|
return 'choice'; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $parameterName |
|
22
|
|
|
* @param mixed $value |
|
23
|
|
|
* @param bool $itemType |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
24 |
|
public function testConditions($parameterName, $value, $itemType = false) |
|
27
|
|
|
{ |
|
28
|
24 |
|
$test = ''; |
|
29
|
24 |
|
if (is_array($value) && 0 < count($value)) { |
|
30
|
24 |
|
$this->addValidationMethod($parameterName, $value); |
|
31
|
24 |
|
$test = sprintf('\'\' !== (%s = self::%s($%s))', self::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName); |
|
32
|
12 |
|
} |
|
33
|
24 |
|
return $test; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $parameterName |
|
38
|
|
|
* @param mixed $value |
|
39
|
|
|
* @param bool $itemType |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
24 |
|
public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false) |
|
43
|
|
|
{ |
|
44
|
24 |
|
return self::getErrorMessageVariableName($parameterName); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $parameterName |
|
49
|
|
|
* @param string[] $choiceNames |
|
50
|
|
|
*/ |
|
51
|
24 |
|
protected function addValidationMethod($parameterName, array $choiceNames) |
|
52
|
|
|
{ |
|
53
|
24 |
|
$attribute = $this->getAttribute(); |
|
54
|
24 |
|
$struct = $attribute->getOwner(); |
|
55
|
24 |
|
$choiceAttributes = []; |
|
56
|
24 |
|
foreach ($choiceNames as $choiceName) { |
|
57
|
24 |
|
if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) { |
|
58
|
24 |
|
$choiceAttributes[] = $choiceAttribute; |
|
59
|
12 |
|
} |
|
60
|
12 |
|
} |
|
61
|
|
|
|
|
62
|
24 |
|
$method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
|
63
|
24 |
|
new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE), |
|
64
|
12 |
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$method |
|
67
|
24 |
|
->addChild('$message = \'\';') |
|
68
|
24 |
|
->addChild('if (is_null($value)) {') |
|
69
|
24 |
|
->addChild($method->getIndentedString('return $message;', 1)) |
|
70
|
24 |
|
->addChild('}') |
|
71
|
24 |
|
->addChild('$properties = ['); |
|
72
|
|
|
|
|
73
|
24 |
|
array_walk($choiceAttributes, function (StructAttribute $choiceAttribute) use ($method) { |
|
74
|
24 |
|
$method->addChild($method->getIndentedString(sprintf('%s,', var_export($choiceAttribute->getCleanName(), true)), 1)); |
|
75
|
24 |
|
}); |
|
76
|
|
|
|
|
77
|
|
|
$method |
|
78
|
24 |
|
->addChild('];') |
|
79
|
24 |
|
->addChild('try {') |
|
80
|
24 |
|
->addChild($method->getIndentedString('foreach ($properties as $property) {', 1)) |
|
81
|
24 |
|
->addChild($method->getIndentedString('if (isset($this->{$property})) {', 2)) |
|
82
|
24 |
|
->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The property %1$s can\\\'t be set as the property %%s is already set. Only one property must be set among these properties: %1$s, %%s.\', $property, implode(\', \', $properties)), __LINE__);', $attribute->getName()), 3)) |
|
83
|
24 |
|
->addChild($method->getIndentedString(sprintf('}'), 2)) |
|
84
|
24 |
|
->addChild($method->getIndentedString('}', 1)) |
|
85
|
24 |
|
->addChild('} catch (\InvalidArgumentException $e) {') |
|
86
|
24 |
|
->addChild($method->getIndentedString('$message = $e->getMessage();', 1)) |
|
87
|
24 |
|
->addChild('}') |
|
88
|
24 |
|
->addChild('return $message;'); |
|
89
|
|
|
|
|
90
|
24 |
|
$this->getMethods()->add($method); |
|
91
|
24 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $parameterName |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
24 |
|
protected function getValidationMethodName($parameterName) |
|
98
|
|
|
{ |
|
99
|
24 |
|
return sprintf('validate%sForChoiceConstraintsFrom%s', ucfirst($parameterName), ucFirst($this->getMethod()->getName())); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $parameterName |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
24 |
|
public static function getErrorMessageVariableName($parameterName) |
|
107
|
|
|
{ |
|
108
|
24 |
|
return sprintf('$%sChoiceErrorMessage', $parameterName); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|