1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\traits; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use nstdio\svg\container\ContainerInterface; |
6
|
|
|
use nstdio\svg\ElementInterface; |
7
|
|
|
use nstdio\svg\ElementStorage; |
8
|
|
|
use nstdio\svg\SVGElement; |
9
|
|
|
use nstdio\svg\XMLDocumentInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ChildTrait |
13
|
|
|
* |
14
|
|
|
* @package nstdio\svg\traits |
15
|
|
|
* @author Edgar Asatryan <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
trait ChildTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ElementStorage |
21
|
|
|
*/ |
22
|
|
|
protected $child; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $name |
26
|
|
|
* |
27
|
|
|
* @return ContainerInterface[] |
28
|
|
|
*/ |
29
|
8 |
|
public function getChild($name) |
30
|
|
|
{ |
31
|
8 |
|
$find = []; |
32
|
|
|
/** @var ContainerInterface $item */ |
33
|
8 |
|
foreach ($this->child as $item) { |
34
|
8 |
|
if (strtolower($item->getName()) === strtolower($name)) { |
35
|
8 |
|
$find[] = $item; |
36
|
8 |
|
} |
37
|
8 |
|
if ($item->hasChild()) { |
38
|
4 |
|
$find = array_merge($find, $item->getChild($name)); |
39
|
4 |
|
} |
40
|
8 |
|
} |
41
|
|
|
|
42
|
8 |
|
return $find; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $id |
47
|
|
|
* |
48
|
|
|
* @return ContainerInterface |
49
|
|
|
*/ |
50
|
4 |
|
public function getChildById($id) |
51
|
|
|
{ |
52
|
4 |
|
$find = null; |
53
|
|
|
/** @var ContainerInterface|SVGElement $item */ |
54
|
4 |
|
foreach ($this->child as $item) { |
55
|
3 |
|
if ($item->id === $id) { |
56
|
3 |
|
$find = $item; |
57
|
3 |
|
} |
58
|
4 |
|
} |
59
|
4 |
|
if ($find === null) { |
60
|
4 |
|
foreach ($this->child as $item) { |
61
|
3 |
|
if ($item->hasChild()) { |
62
|
3 |
|
$find = $item->getChildById($id); |
63
|
3 |
|
} |
64
|
4 |
|
} |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
return $find; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ContainerInterface|null |
72
|
|
|
*/ |
73
|
66 |
|
public function getFirstChild() |
74
|
|
|
{ |
75
|
66 |
|
if (!$this->hasChild()) { |
76
|
1 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
66 |
|
return $this->child[0]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
66 |
|
public function hasChild() |
86
|
|
|
{ |
87
|
66 |
|
return count($this->child) > 0; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ContainerInterface[]|ElementStorage |
92
|
|
|
*/ |
93
|
2 |
|
public function getChildren() |
94
|
|
|
{ |
95
|
2 |
|
return $this->child; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $index |
100
|
|
|
* |
101
|
|
|
* @return ContainerInterface|SVGElement|null |
102
|
|
|
*/ |
103
|
9 |
|
public function getChildAtIndex($index) |
104
|
|
|
{ |
105
|
9 |
|
return $this->child[$index]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ElementInterface $child |
110
|
|
|
*/ |
111
|
11 |
|
public function removeChild(ElementInterface $child) |
112
|
|
|
{ |
113
|
11 |
|
$this->child->remove($child); |
114
|
|
|
/** @var XMLDocumentInterface $element */ |
115
|
11 |
|
$element = $this->getElement(); |
|
|
|
|
116
|
11 |
|
if ($element instanceof \DOMNode) { |
117
|
3 |
|
$element->removeChild($child->getElement()->getElement()); |
|
|
|
|
118
|
3 |
|
} else { |
119
|
9 |
|
$element->removeNode($child->getElement()); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.