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 Enumeration |
16
|
|
|
*/ |
17
|
52 |
|
public function applyRule($parameterName, $value, $itemType = false) |
18
|
|
|
{ |
19
|
52 |
|
if ($this->getAttribute()->isArray()) { |
20
|
48 |
|
$model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute()); |
21
|
48 |
|
$itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName())); |
22
|
48 |
|
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
|
44 |
|
$this->getMethod()->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName)); |
36
|
44 |
|
$this->getRules()->getItemTypeRule()->applyRule($itemName, true); |
37
|
44 |
|
$this->getMethod()->addChild('}'); |
38
|
|
|
} |
39
|
36 |
|
} |
40
|
52 |
|
return $this; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.