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