1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mikael |
5
|
|
|
* Date: 21/01/19 |
6
|
|
|
* Time: 12:32 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
10
|
|
|
|
11
|
|
|
use WsdlToPhp\PackageGenerator\File\StructEnum; |
12
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct; |
13
|
|
|
|
14
|
|
|
abstract class AbstractSetOfValuesRule extends AbstractRule |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Name of the validation rule |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
abstract protected function name(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Must check the attribute validity according to the current rule |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
abstract protected function mustApplyRuleOnAttribute(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::applyRule() |
30
|
|
|
* @param string $parameterName |
31
|
|
|
* @param mixed $value |
32
|
|
|
* @param bool $itemType |
33
|
|
|
* @return ListRule |
34
|
|
|
*/ |
35
|
114 |
|
public function applyRule($parameterName, $value, $itemType = false) |
36
|
|
|
{ |
37
|
114 |
|
if ($this->mustApplyRuleOnAttribute()) { |
38
|
114 |
|
$model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
39
|
114 |
|
$itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
40
|
114 |
|
$this->getMethod()->addChild(sprintf('// validation for constraint: %s', $this->name())); |
41
|
114 |
|
if ($model instanceof Struct) { |
42
|
30 |
|
$this->getMethod() |
43
|
30 |
|
->addChild('$invalidValues = array();') |
44
|
30 |
|
->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName)) |
45
|
30 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $itemName), 1)) |
46
|
30 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2)) |
47
|
30 |
|
->addChild($this->getMethod()->getIndentedString('}', 1)) |
48
|
30 |
|
->addChild('}') |
49
|
30 |
|
->addChild('if (!empty($invalidValues)) {') |
50
|
30 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value(s) "%%s" is/are invalid, please use one of: %%s\', implode(\', \', $invalidValues), implode(\', \', %s::%s())), __LINE__);', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1)) |
51
|
30 |
|
->addChild('}'); |
52
|
15 |
|
} else { |
53
|
108 |
|
$this->getMethod()->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName)); |
54
|
108 |
|
$this->getRules()->getItemTypeRule()->applyRule($itemName, true); |
55
|
108 |
|
$this->getMethod()->addChild('}'); |
56
|
|
|
} |
57
|
57 |
|
} |
58
|
114 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|