|
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
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
7 |
|
public function getChild($name) |
|
28
|
|
|
{ |
|
29
|
7 |
|
$find = []; |
|
30
|
|
|
/** @var ContainerInterface $item */ |
|
31
|
7 |
|
foreach ($this->child as $item) { |
|
32
|
7 |
|
if (strtolower($item->getName()) === strtolower($name)) { |
|
33
|
7 |
|
$find[] = $item; |
|
34
|
7 |
|
} |
|
35
|
7 |
|
if ($item->hasChild()) { |
|
36
|
3 |
|
$find = array_merge($find, $item->getChild($name)); |
|
37
|
3 |
|
} |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
7 |
|
return $find; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function getChildById($id) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$find = null; |
|
49
|
|
|
/** @var ContainerInterface|SVGElement $item */ |
|
50
|
4 |
|
foreach ($this->child as $item) { |
|
51
|
3 |
|
if ($item->id === $id) { |
|
52
|
3 |
|
$find = $item; |
|
53
|
3 |
|
} |
|
54
|
4 |
|
} |
|
55
|
4 |
|
if ($find === null) { |
|
56
|
4 |
|
foreach ($this->child as $item) { |
|
57
|
3 |
|
if ($item->hasChild()) { |
|
58
|
3 |
|
$find = $item->getChildById($id); |
|
59
|
3 |
|
} |
|
60
|
4 |
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $find; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritdoc |
|
68
|
|
|
*/ |
|
69
|
46 |
|
public function getFirstChild() |
|
70
|
|
|
{ |
|
71
|
46 |
|
if (!$this->hasChild()) { |
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
46 |
|
return $this->child[0]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritdoc |
|
80
|
|
|
*/ |
|
81
|
46 |
|
public function hasChild() |
|
82
|
|
|
{ |
|
83
|
46 |
|
return count($this->child) > 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function getChildren() |
|
90
|
|
|
{ |
|
91
|
2 |
|
return $this->child; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function getChildAtIndex($index) |
|
98
|
|
|
{ |
|
99
|
6 |
|
return $this->child[$index]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function removeChild(ElementInterface $child) |
|
106
|
|
|
{ |
|
107
|
3 |
|
$this->child->remove($child); |
|
108
|
|
|
/** @var XMLDocumentInterface $element */ |
|
109
|
3 |
|
$element = $this->getElement(); |
|
|
|
|
|
|
110
|
3 |
|
if ($element instanceof \DOMNode) { |
|
111
|
|
|
return $element->removeChild($child->getElement()->getElement()); |
|
|
|
|
|
|
112
|
|
|
} else { |
|
113
|
3 |
|
return $element->removeNode($child->getElement()); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
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
Idableprovides a methodequalsIdthat 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.