1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
6
|
|
|
|
7
|
|
|
use WsdlToPhp\PackageGenerator\File\AbstractModelFile; |
8
|
|
|
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter; |
9
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute; |
10
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
11
|
|
|
|
12
|
|
|
final class ChoiceRule extends AbstractRule |
13
|
|
|
{ |
14
|
|
|
public const NAME = 'choice'; |
15
|
|
|
|
16
|
8 |
|
public function name(): string |
17
|
|
|
{ |
18
|
8 |
|
return self::NAME; |
19
|
|
|
} |
20
|
|
|
|
21
|
8 |
|
public function testConditions(string $parameterName, $value, bool $itemType = false): string |
22
|
|
|
{ |
23
|
8 |
|
$test = ''; |
24
|
8 |
|
if (is_array($value) && 0 < count($value)) { |
25
|
8 |
|
$this->addValidationMethod($parameterName, $value); |
26
|
8 |
|
$test = sprintf('\'\' !== (%s = self::%s($%s))', self::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName); |
27
|
|
|
} |
28
|
|
|
|
29
|
8 |
|
return $test; |
30
|
|
|
} |
31
|
|
|
|
32
|
8 |
|
public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string |
33
|
|
|
{ |
34
|
8 |
|
return self::getErrorMessageVariableName($parameterName); |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
public static function getErrorMessageVariableName(string $parameterName): string |
38
|
|
|
{ |
39
|
8 |
|
return sprintf('$%sChoiceErrorMessage', $parameterName); |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
protected function addValidationMethod(string $parameterName, array $choiceNames) |
43
|
|
|
{ |
44
|
8 |
|
$attribute = $this->getAttribute(); |
45
|
8 |
|
$struct = $attribute->getOwner(); |
46
|
8 |
|
$choiceAttributes = []; |
47
|
8 |
|
foreach ($choiceNames as $choiceName) { |
48
|
8 |
|
if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) { |
49
|
8 |
|
$choiceAttributes[] = $choiceAttribute; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
$method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
54
|
8 |
|
new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE), |
55
|
8 |
|
], AbstractModelFile::TYPE_STRING); |
56
|
|
|
|
57
|
8 |
|
$method |
58
|
8 |
|
->addChild('$message = \'\';') |
59
|
8 |
|
->addChild('if (is_null($value)) {') |
60
|
8 |
|
->addChild($method->getIndentedString('return $message;', 1)) |
61
|
8 |
|
->addChild('}') |
62
|
8 |
|
->addChild('$properties = [') |
63
|
8 |
|
; |
64
|
|
|
|
65
|
8 |
|
array_walk($choiceAttributes, function (StructAttribute $choiceAttribute) use ($method) { |
66
|
8 |
|
$method->addChild($method->getIndentedString(sprintf('%s,', var_export($choiceAttribute->getCleanName(), true)), 1)); |
67
|
8 |
|
}); |
68
|
|
|
|
69
|
8 |
|
$method |
70
|
8 |
|
->addChild('];') |
71
|
8 |
|
->addChild('try {') |
72
|
8 |
|
->addChild($method->getIndentedString('foreach ($properties as $property) {', 1)) |
73
|
8 |
|
->addChild($method->getIndentedString('if (isset($this->{$property})) {', 2)) |
74
|
8 |
|
->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)) |
75
|
8 |
|
->addChild($method->getIndentedString(sprintf('}'), 2)) |
76
|
8 |
|
->addChild($method->getIndentedString('}', 1)) |
77
|
8 |
|
->addChild('} catch (InvalidArgumentException $e) {') |
78
|
8 |
|
->addChild($method->getIndentedString('$message = $e->getMessage();', 1)) |
79
|
8 |
|
->addChild('}') |
80
|
8 |
|
->addChild('') |
81
|
8 |
|
->addChild('return $message;') |
82
|
8 |
|
; |
83
|
|
|
|
84
|
8 |
|
$this->getMethods()->add($method); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|