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
|
|
|
public function __construct($list = null, $parentNode = null) |
24
|
|
|
{ |
25
|
|
|
parent::__construct( $list ); |
26
|
|
|
$this->parentNode = $parentNode; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __set($name, $value) |
30
|
|
|
{ |
31
|
|
|
switch ($name) { |
32
|
|
|
case 'parentNode': |
33
|
|
|
$this->parentNode = $value; |
34
|
|
|
foreach ($this as $child) { |
35
|
|
|
$child->parentNode = $this->parentNode; |
36
|
|
|
} |
37
|
|
|
break; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __get($name) |
42
|
|
|
{ |
43
|
|
|
switch ($name) { |
44
|
|
|
case 'parentNode': |
45
|
|
|
return $this->parentNode; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function offsetSet($name, $value) |
51
|
|
|
{ |
52
|
|
|
if (!($value instanceof \arc\tree\NamedNode)) { |
53
|
|
|
$value = new \arc\tree\NamedNode( $name, $this->parentNode, null, $value ); |
54
|
|
|
} |
55
|
|
|
$value->parentNode = $this->parentNode; |
56
|
|
|
if ($this->offsetExists( $name )) { |
57
|
|
|
$old = $this->offsetGet( $name ); |
58
|
|
|
if ($old !== $value) { |
59
|
|
|
$old->parentNode = null; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
parent::offsetSet($name, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function offsetUnset($name) |
66
|
|
|
{ |
67
|
|
|
if ($this->offsetExists( $name )) { |
68
|
|
|
$node = $this->offsetGet( $name ); |
69
|
|
|
$node->parentNode = null; |
70
|
|
|
} |
71
|
|
|
parent::offsetUnset( $name ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function __clone() |
75
|
|
|
{ |
76
|
|
|
$this->parentNode = null; |
77
|
|
|
foreach ((array) $this as $name => $child) { |
78
|
|
|
$clone = clone $child; |
79
|
|
|
$clone->parentNode = $this->parentNode; |
80
|
|
|
parent::offsetSet( $name, $clone ); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function exchangeArray($input) |
85
|
|
|
{ |
86
|
|
|
$oldArray = $this->getArrayCopy(); |
87
|
|
|
foreach ($oldArray as $node) { |
88
|
|
|
$node->parentNode = null; // removes them from the childNodes list as well |
89
|
|
|
} |
90
|
|
|
foreach ($input as $name => $node) { |
91
|
|
|
$this->offsetSet( $name, $node ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.