|
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\PhpGenerator\Element\PhpFunctionParameter; |
|
9
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Gathers [min|max|]Length rules. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractLengthRule extends AbstractMinMaxRule |
|
15
|
|
|
{ |
|
16
|
18 |
|
final public function testConditions(string $parameterName, $value, bool $itemType = false): string |
|
17
|
|
|
{ |
|
18
|
18 |
|
if ($itemType || !$this->getAttribute()->isArray()) { |
|
19
|
18 |
|
$test = sprintf(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d', $parameterName, $value, $this->symbol()); |
|
20
|
|
|
} else { |
|
21
|
4 |
|
$this->addValidationMethod($parameterName, $value); |
|
22
|
4 |
|
$test = sprintf('\'\' !== (%s = self::%s($%s))', $this->getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
18 |
|
return $test; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
18 |
|
final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string |
|
29
|
|
|
{ |
|
30
|
18 |
|
if ($itemType || !$this->getAttribute()->isArray()) { |
|
31
|
18 |
|
$message = sprintf('sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen((string) $%s))', $this->comparisonString(), is_array($value) ? implode(',', array_unique($value)) : $value, $parameterName); |
|
32
|
|
|
} else { |
|
33
|
4 |
|
$message = $this->getErrorMessageVariableName($parameterName); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
18 |
|
return $message; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public function getErrorMessageVariableName(string $parameterName): string |
|
40
|
|
|
{ |
|
41
|
4 |
|
return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name())); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
protected function addValidationMethod(string $parameterName, $value): void |
|
45
|
|
|
{ |
|
46
|
4 |
|
$method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
|
47
|
4 |
|
new PhpFunctionParameter('values', PhpFunctionParameter::NO_VALUE), |
|
48
|
|
|
], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true); |
|
49
|
4 |
|
$itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
|
50
|
|
|
|
|
51
|
|
|
$method |
|
52
|
4 |
|
->addChild('$message = \'\';') |
|
53
|
4 |
|
->addChild('$invalidValues = [];') |
|
54
|
4 |
|
->addChild(sprintf('foreach ($values as $%s) {', $itemName)) |
|
55
|
4 |
|
->addChild($method->getIndentedString($this->validationRuleComment($value), 1)) |
|
56
|
4 |
|
->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1)) |
|
57
|
4 |
|
->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2)) |
|
58
|
4 |
|
->addChild($method->getIndentedString('}', 1)) |
|
59
|
4 |
|
->addChild('}') |
|
60
|
4 |
|
->addChild('if (!empty($invalidValues)) {') |
|
61
|
4 |
|
->addChild($method->getIndentedString(sprintf('$message = sprintf(\'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s\', implode(\', \', $invalidValues));', $this->comparisonString(), $value), 1)) |
|
62
|
4 |
|
->addChild('}') |
|
63
|
4 |
|
->addChild('unset($invalidValues);') |
|
64
|
4 |
|
->addChild('') |
|
65
|
4 |
|
->addChild('return $message;') |
|
66
|
|
|
; |
|
67
|
4 |
|
$this->getMethods()->add($method); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
protected function getValidationMethodName(string $parameterName): string |
|
71
|
|
|
{ |
|
72
|
4 |
|
return sprintf('validate%sFor%sConstraintFrom%s', ucfirst($parameterName), ucfirst($this->name()), ucfirst($this->getMethod()->getName())); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|