|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Wszetko\Sitemap\Items\DataTypes; |
|
5
|
|
|
|
|
6
|
|
|
use Wszetko\Sitemap\Interfaces\DataType; |
|
7
|
|
|
use Wszetko\Sitemap\Traits\Domain; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ArrayType |
|
11
|
|
|
* |
|
12
|
|
|
* @package Wszetko\Sitemap\Items\DataTypes |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArrayType extends AbstractDataType |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Wszetko\Sitemap\Items\DataTypes\AbstractDataType |
|
18
|
|
|
*/ |
|
19
|
|
|
private $baseDataType; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $maxElements; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ArrayType constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param $name |
|
30
|
|
|
* @param $dataType |
|
31
|
|
|
*/ |
|
32
|
41 |
|
public function __construct($name, $dataType) |
|
33
|
|
|
{ |
|
34
|
41 |
|
parent::__construct($name); |
|
35
|
|
|
|
|
36
|
41 |
|
$this->baseDataType = new $dataType($this->getName()); |
|
37
|
41 |
|
$this->value = []; |
|
38
|
41 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return \Wszetko\Sitemap\Items\DataTypes\AbstractDataType |
|
42
|
|
|
*/ |
|
43
|
41 |
|
public function getBaseDataType(): AbstractDataType |
|
44
|
|
|
{ |
|
45
|
41 |
|
return $this->baseDataType; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return int|null |
|
50
|
|
|
*/ |
|
51
|
8 |
|
public function getMaxElements(): ?int |
|
52
|
|
|
{ |
|
53
|
8 |
|
return $this->maxElements; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param int $maxElements |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
60
|
|
|
*/ |
|
61
|
34 |
|
public function setMaxElements(int $maxElements): self |
|
62
|
|
|
{ |
|
63
|
34 |
|
$this->maxElements = $maxElements; |
|
64
|
|
|
|
|
65
|
34 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $value |
|
70
|
|
|
* @param mixed ...$parameters |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Wszetko\Sitemap\Interfaces\DataType |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function setValue($value, ...$parameters): DataType |
|
75
|
|
|
{ |
|
76
|
2 |
|
if (is_array($value)) { |
|
77
|
2 |
|
foreach ($value as $val) { |
|
78
|
2 |
|
$this->addValue($val, $parameters); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
1 |
|
$this->addValue($value, $parameters); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
return $this; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $value |
|
89
|
|
|
* @param mixed ...$parameters |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function addValue($value, ...$parameters) |
|
92
|
|
|
{ |
|
93
|
9 |
|
if (is_array($value)) { |
|
94
|
3 |
|
foreach ($value as $val) { |
|
95
|
3 |
|
$this->addValue($val, $parameters[0]); |
|
96
|
|
|
} |
|
97
|
|
|
} else { |
|
98
|
9 |
|
$var = clone $this->getBaseDataType(); |
|
99
|
9 |
|
$var->setValue($value, $parameters[0]); |
|
100
|
|
|
|
|
101
|
9 |
|
if (!is_null($var->getValue())) { |
|
102
|
9 |
|
$this->value[] = $var; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
9 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
8 |
|
public function getValue() |
|
111
|
|
|
{ |
|
112
|
8 |
|
$values = parent::getValue(); |
|
113
|
|
|
|
|
114
|
8 |
|
if (empty($values)) { |
|
115
|
2 |
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
$result = []; |
|
119
|
|
|
|
|
120
|
8 |
|
foreach ($values as $element) { |
|
|
|
|
|
|
121
|
8 |
|
$this->propagateDomain($element); |
|
122
|
8 |
|
$value = $element->getValue(); |
|
123
|
|
|
|
|
124
|
8 |
|
if (is_array($value) && !empty(array_values($value)[0])) { |
|
125
|
2 |
|
$result[array_key_first($value)] = array_values($value)[0]; |
|
126
|
6 |
|
} elseif (!empty($value)) { |
|
127
|
8 |
|
$result[] = $value; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
8 |
|
if ($this->getMaxElements()) { |
|
|
|
|
|
|
132
|
4 |
|
$result = array_slice($result, 0, $this->getMaxElements()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
} |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.