1 | <?php |
||
2 | |||
3 | |||
4 | namespace Zenwalker\CommerceML\Model; |
||
5 | |||
6 | |||
7 | /** |
||
8 | * Class Group |
||
9 | * |
||
10 | * @package Zenwalker\CommerceML\Model |
||
11 | * @property Group parent |
||
12 | * @property Group[] children |
||
13 | */ |
||
14 | class Group extends Simple |
||
15 | { |
||
16 | /** |
||
17 | * @var Group[] |
||
18 | */ |
||
19 | protected $children = []; |
||
20 | |||
21 | /** |
||
22 | * @var Group |
||
23 | */ |
||
24 | protected $parent; |
||
25 | |||
26 | /** |
||
27 | * @return Group[] |
||
28 | */ |
||
29 | 5 | public function getChildren() |
|
30 | { |
||
31 | 5 | if (empty($this->children) && $this->xml->Группы) { |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
32 | 4 | foreach ($this->xml->Группы->Группа as $group) { |
|
33 | 4 | $this->children[] = new Group($this->owner, $group); |
|
34 | } |
||
35 | } |
||
36 | 5 | return $this->children; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return Group |
||
41 | */ |
||
42 | 1 | public function getParent() |
|
43 | { |
||
44 | 1 | if (!$this->parent) { |
|
45 | 1 | $parent = $this->xpath('../..')[0]; |
|
46 | 1 | if ($parent->getName() === 'Группа') { |
|
47 | 1 | $this->parent = new Group($this->owner, $parent); |
|
48 | } |
||
49 | } |
||
50 | 1 | return $this->parent; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $id |
||
55 | * @return null|Group |
||
56 | */ |
||
57 | 5 | public function getChildById($id) |
|
58 | { |
||
59 | 5 | foreach ($this->getChildren() as $child) { |
|
60 | 4 | if ($child->id === $id) { |
|
61 | 2 | return $child; |
|
62 | } |
||
63 | |||
64 | 3 | if ($subChild = $child->getChildById($id)) { |
|
65 | return $subChild; |
||
66 | } |
||
67 | } |
||
68 | 5 | return null; |
|
69 | } |
||
70 | } |