|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\File\StructEnum; |
|
6
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct; |
|
7
|
|
|
|
|
8
|
|
|
class ArrayRule extends AbstractRule |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @see \WsdlToPhp\PackageGenerator\File\Validation\AbstractValidation::addRule() |
|
12
|
|
|
* @param string $parameterName |
|
13
|
|
|
* @param mixed $value |
|
14
|
|
|
* @param bool $itemType |
|
15
|
|
|
* @return ArrayRule |
|
16
|
|
|
*/ |
|
17
|
60 |
|
public function applyRule($parameterName, $value, $itemType = false) |
|
18
|
|
|
{ |
|
19
|
60 |
|
if ($this->getAttribute()->isArray()) { |
|
20
|
60 |
|
$model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
|
21
|
60 |
|
$itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
|
22
|
60 |
|
if ($model instanceof Struct) { |
|
23
|
12 |
|
$this |
|
24
|
16 |
|
->getMethod() |
|
25
|
16 |
|
->addChild('$invalidValues = array();') |
|
26
|
16 |
|
->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName)) |
|
27
|
16 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $itemName), 1)) |
|
28
|
16 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('$invalidValues[] = var_export($%s);', $itemName), 2)) |
|
29
|
16 |
|
->addChild($this->getMethod()->getIndentedString('}', 1)) |
|
30
|
16 |
|
->addChild('}') |
|
31
|
16 |
|
->addChild('if (!empty($invalidValues)) {') |
|
32
|
16 |
|
->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)) |
|
33
|
16 |
|
->addChild('}'); |
|
34
|
12 |
|
} else { |
|
35
|
56 |
|
$this->getMethod()->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName)); |
|
36
|
56 |
|
$this->getRules()->getItemTypeRule()->applyRule($itemName, true); |
|
37
|
56 |
|
$this->getMethod()->addChild('}'); |
|
38
|
|
|
} |
|
39
|
45 |
|
} |
|
40
|
60 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|