Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
37 / 37 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
AbstractSetOfValuesRule | |
100.00% |
37 / 37 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
testConditions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
exceptionMessageOnTestFailure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getErrorMessageVariableName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParameterPassedValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mustApplyRuleOnAttribute | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addValidationMethod | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
2 |
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\Model\Struct; |
9 | use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter; |
10 | use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
11 | |
12 | abstract class AbstractSetOfValuesRule extends AbstractRule |
13 | { |
14 | public function testConditions(string $parameterName, $value, bool $itemType = false): string |
15 | { |
16 | $test = ''; |
17 | if ($this->mustApplyRuleOnAttribute()) { |
18 | $this->addValidationMethod($parameterName, $value); |
19 | $test = sprintf('\'\' !== (%s = self::%s(%s))', static::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), static::getParameterPassedValue($parameterName)); |
20 | } |
21 | |
22 | return $test; |
23 | } |
24 | |
25 | public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string |
26 | { |
27 | return static::getErrorMessageVariableName($parameterName); |
28 | } |
29 | |
30 | public static function getErrorMessageVariableName(string $parameterName): string |
31 | { |
32 | return sprintf('$%sArrayErrorMessage', $parameterName); |
33 | } |
34 | |
35 | public static function getParameterPassedValue(string $parameterName): string |
36 | { |
37 | return sprintf('$%s', $parameterName); |
38 | } |
39 | |
40 | /** |
41 | * Must check the attribute validity according to the current rule. |
42 | */ |
43 | abstract protected function mustApplyRuleOnAttribute(): bool; |
44 | |
45 | protected function addValidationMethod(string $parameterName, $value) |
46 | { |
47 | $method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
48 | new PhpFunctionParameter('values', [], '?array'), |
49 | ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true); |
50 | $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
51 | $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
52 | $rules = clone $this->getRules(); |
53 | |
54 | if ($model instanceof Struct) { |
55 | $rule = $rules->getEnumerationRule(); |
56 | } else { |
57 | $rule = $rules->setMethod($method)->getItemTypeRule(); |
58 | } |
59 | |
60 | $method |
61 | ->addChild('if (!is_array($values)) {') |
62 | ->addChild($method->getIndentedString('return \'\';', 1)) |
63 | ->addChild('}') |
64 | ->addChild('$message = \'\';') |
65 | ->addChild('$invalidValues = [];') |
66 | ->addChild(sprintf('foreach ($values as $%s) {', $itemName)) |
67 | ->addChild($method->getIndentedString($rule->validationRuleComment($value), 1)) |
68 | ->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1)) |
69 | ->addChild($method->getIndentedString(sprintf('$invalidValues[] = %s;', sprintf('is_object($%1$s) ? get_class($%1$s) : sprintf(\'%%s(%%s)\', gettype($%1$s), var_export($%1$s, true))', $itemName)), 2)) |
70 | ->addChild($method->getIndentedString('}', 1)) |
71 | ->addChild('}') |
72 | ->addChild('if (!empty($invalidValues)) {') |
73 | ->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1)) |
74 | ->addChild('}') |
75 | ->addChild('unset($invalidValues);') |
76 | ->addChild('') |
77 | ->addChild('return $message;') |
78 | ; |
79 | $this->getMethods()->add($method); |
80 | } |
81 | } |