|
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
|
|
|
use Wszetko\Sitemap\Traits\Required; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AbstractDataType |
|
12
|
|
|
* |
|
13
|
|
|
* @package Wszetko\Sitemap\Items\DataTypes |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractDataType implements DataType |
|
16
|
|
|
{ |
|
17
|
|
|
use Required; |
|
18
|
|
|
use Domain; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $value; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $attributes = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* AbstractDataType constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $name |
|
39
|
|
|
*/ |
|
40
|
57 |
|
public function __construct($name) |
|
41
|
|
|
{ |
|
42
|
57 |
|
$this->name = $name; |
|
43
|
57 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
41 |
|
public function getName(): string |
|
49
|
|
|
{ |
|
50
|
41 |
|
return $this->name; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
38 |
|
public function getValue() |
|
57
|
|
|
{ |
|
58
|
38 |
|
$value = $this->value; |
|
59
|
38 |
|
$attributes = $this->getAttributes(); |
|
60
|
|
|
|
|
61
|
38 |
|
if (!empty($attributes)) { |
|
62
|
8 |
|
return [$value => $attributes]; |
|
63
|
|
|
} else { |
|
64
|
37 |
|
return $value; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $value |
|
70
|
|
|
* @param mixed ...$parameters |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Wszetko\Sitemap\Interfaces\DataType |
|
73
|
|
|
*/ |
|
74
|
56 |
|
public function setValue($value, ...$parameters): DataType |
|
75
|
|
|
{ |
|
76
|
56 |
|
$this->value = $value; |
|
77
|
|
|
|
|
78
|
56 |
|
foreach ($parameters[0] as $key => $attribute) { |
|
79
|
|
|
|
|
80
|
10 |
|
if (!empty($attribute)) { |
|
81
|
9 |
|
$attr = array_keys($this->attributes)[$key] ?? null; |
|
82
|
|
|
|
|
83
|
9 |
|
if ($attr) { |
|
84
|
10 |
|
$this->attributes[$attr]->setValue($attribute); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
56 |
|
return $this; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
57 |
|
public function addAttributes($attributes): self |
|
93
|
|
|
{ |
|
94
|
57 |
|
foreach ($attributes as $name => $dataType) { |
|
95
|
27 |
|
$this->attributes[$name] = new $dataType($name); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
57 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
27 |
|
public function getAttribute($name) |
|
102
|
|
|
{ |
|
103
|
27 |
|
return isset($this->attributes[$name]) ? $this->attributes[$name] : null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
44 |
|
public function getAttributes(): array |
|
107
|
|
|
{ |
|
108
|
44 |
|
$attributes = []; |
|
109
|
|
|
|
|
110
|
44 |
|
foreach ($this->attributes as $name => $value) { |
|
111
|
9 |
|
$this->propagateDomain($value); |
|
112
|
|
|
// var_dump($this->getDomain(), $value->getDomain()); |
|
113
|
9 |
|
if (!empty($value->getValue())) { |
|
114
|
9 |
|
$attributes[$name] = $value->getValue(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
44 |
|
return $attributes; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
14 |
|
public function propagateDomain(object &$target): void |
|
122
|
|
|
{ |
|
123
|
14 |
|
if ($this->getDomain() !== null) { |
|
124
|
5 |
|
$target->setDomain($this->getDomain()); |
|
125
|
|
|
} |
|
126
|
14 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* To clone properties of object with no reference |
|
130
|
|
|
*/ |
|
131
|
9 |
|
public function __clone() |
|
132
|
|
|
{ |
|
133
|
9 |
|
foreach ($this->attributes as $attribute => $value) { |
|
134
|
3 |
|
if (gettype($value) == 'object') { |
|
135
|
3 |
|
$this->attributes[$attribute] = clone $this->attributes[$attribute]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
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.