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
|
64 |
|
public function testConditions(string $parameterName, $value, bool $itemType = false): string |
15
|
|
|
{ |
16
|
64 |
|
$test = ''; |
17
|
64 |
|
if ($this->mustApplyRuleOnAttribute()) { |
18
|
64 |
|
$this->addValidationMethod($parameterName, $value); |
19
|
64 |
|
$test = sprintf('\'\' !== (%s = self::%s(%s))', static::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), static::getParameterPassedValue($parameterName)); |
20
|
|
|
} |
21
|
|
|
|
22
|
64 |
|
return $test; |
23
|
|
|
} |
24
|
|
|
|
25
|
64 |
|
public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string |
26
|
|
|
{ |
27
|
64 |
|
return static::getErrorMessageVariableName($parameterName); |
28
|
|
|
} |
29
|
|
|
|
30
|
64 |
|
public static function getErrorMessageVariableName(string $parameterName): string |
31
|
|
|
{ |
32
|
64 |
|
return sprintf('$%sArrayErrorMessage', $parameterName); |
33
|
|
|
} |
34
|
|
|
|
35
|
60 |
|
public static function getParameterPassedValue(string $parameterName): string |
36
|
|
|
{ |
37
|
60 |
|
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
|
64 |
|
protected function addValidationMethod(string $parameterName, $value) |
46
|
|
|
{ |
47
|
64 |
|
$method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
48
|
64 |
|
new PhpFunctionParameter('values', [], '?array'), |
49
|
64 |
|
], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true); |
50
|
64 |
|
$model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
51
|
64 |
|
$itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
52
|
64 |
|
$rules = clone $this->getRules(); |
53
|
|
|
|
54
|
64 |
|
if ($model instanceof Struct) { |
55
|
14 |
|
$rule = $rules->getEnumerationRule(); |
56
|
|
|
} else { |
57
|
58 |
|
$rule = $rules->setMethod($method)->getItemTypeRule(); |
58
|
|
|
} |
59
|
|
|
|
60
|
64 |
|
$method |
61
|
64 |
|
->addChild('if (!is_array($values)) {') |
62
|
64 |
|
->addChild($method->getIndentedString('return \'\';', 1)) |
63
|
64 |
|
->addChild('}') |
64
|
64 |
|
->addChild('$message = \'\';') |
65
|
64 |
|
->addChild('$invalidValues = [];') |
66
|
64 |
|
->addChild(sprintf('foreach ($values as $%s) {', $itemName)) |
67
|
64 |
|
->addChild($method->getIndentedString($rule->validationRuleComment($value), 1)) |
68
|
64 |
|
->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1)) |
69
|
64 |
|
->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
|
64 |
|
->addChild($method->getIndentedString('}', 1)) |
71
|
64 |
|
->addChild('}') |
72
|
64 |
|
->addChild('if (!empty($invalidValues)) {') |
73
|
64 |
|
->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1)) |
74
|
64 |
|
->addChild('}') |
75
|
64 |
|
->addChild('unset($invalidValues);') |
76
|
64 |
|
->addChild('') |
77
|
64 |
|
->addChild('return $message;') |
78
|
64 |
|
; |
79
|
64 |
|
$this->getMethods()->add($method); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|