1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ariadne Component Library. |
5
|
|
|
* |
6
|
|
|
* (c) Muze <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace arc\tree; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements an ArrayObject with constraint that a value is always a NamedNode and its key is always the name |
16
|
|
|
* of that node |
17
|
|
|
* @property \arc\tree\NamedNode $parentNode |
18
|
|
|
*/ |
19
|
|
|
class NamedNodeList extends \ArrayObject |
20
|
|
|
{ |
21
|
|
|
private $parentNode = null; |
22
|
|
|
|
23
|
16 |
|
public function __construct($list = null, $parentNode = null) |
24
|
|
|
{ |
25
|
16 |
|
parent::__construct( $list ); |
26
|
16 |
|
$this->parentNode = $parentNode; |
27
|
16 |
|
} |
28
|
|
|
|
29
|
2 |
|
public function __set($name, $value) |
30
|
|
|
{ |
31
|
|
|
switch ($name) { |
32
|
2 |
|
case 'parentNode': |
33
|
2 |
|
$this->parentNode = $value; |
34
|
2 |
|
foreach ($this as $child) { |
35
|
2 |
|
$child->parentNode = $this->parentNode; |
36
|
|
|
} |
37
|
2 |
|
break; |
38
|
|
|
} |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
public function __get($name) |
42
|
|
|
{ |
43
|
|
|
switch ($name) { |
44
|
|
|
case 'parentNode': |
45
|
|
|
return $this->parentNode; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
16 |
|
public function offsetSet($name, $value) |
51
|
|
|
{ |
52
|
16 |
|
if (!($value instanceof \arc\tree\NamedNode)) { |
53
|
6 |
|
$value = new \arc\tree\NamedNode( $name, $this->parentNode, null, $value ); |
54
|
|
|
} |
55
|
16 |
|
if ($value->parentNode != $this->parentNode) { |
56
|
|
|
$value->parentNode = $this->parentNode; |
57
|
|
|
} |
58
|
16 |
View Code Duplication |
if ($this->offsetExists( $name )) { |
|
|
|
|
59
|
2 |
|
$old = $this->offsetGet( $name ); |
60
|
2 |
|
if ($old !== $value) { |
61
|
2 |
|
$old->parentNode = null; |
62
|
|
|
} |
63
|
|
|
} |
64
|
16 |
|
parent::offsetSet($name, $value); |
65
|
16 |
|
} |
66
|
|
|
|
67
|
4 |
|
public function offsetUnset($name) |
68
|
|
|
{ |
69
|
4 |
View Code Duplication |
if ($this->offsetExists( $name )) { |
|
|
|
|
70
|
4 |
|
$node = $this->offsetGet( $name ); |
71
|
4 |
|
if ($node->parentNode) { |
72
|
|
|
$node->parentNode = null; |
73
|
|
|
} |
74
|
|
|
} |
75
|
4 |
|
parent::offsetUnset( $name ); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
2 |
|
public function __clone() |
79
|
|
|
{ |
80
|
2 |
|
$this->parentNode = null; |
81
|
2 |
|
foreach ((array) $this as $name => $child) { |
82
|
2 |
|
$clone = clone $child; |
83
|
2 |
|
$clone->parentNode = $this->parentNode; |
84
|
2 |
|
parent::offsetSet( $name, $clone ); |
|
|
|
|
85
|
|
|
} |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
public function exchangeArray($input) |
89
|
|
|
{ |
90
|
|
|
$oldArray = $this->getArrayCopy(); |
91
|
|
|
foreach ($oldArray as $node) { |
92
|
|
|
$node->parentNode = null; // removes them from the childNodes list as well |
93
|
|
|
} |
94
|
|
|
foreach ($input as $name => $node) { |
95
|
|
|
$this->offsetSet( $name, $node ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.