1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\JsonDocument; |
4
|
|
|
|
5
|
|
|
class ObjectNode extends AbstractParent |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var AttributeNode[] |
9
|
|
|
*/ |
10
|
|
|
protected $attributes = array(); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param AttributeNode $attribute |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function addAttribute(AttributeNode $attribute) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$name = $attribute->getName(); |
19
|
|
|
if (isset($this->attributes[$name])) { |
20
|
|
|
throw new \InvalidArgumentException(sprintf("There is allready a attribute with this name '%s'!", $name)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->checkAttribute($attribute); |
24
|
|
|
$this->removeAttributeFromParent($attribute); |
25
|
|
|
Document::setProperty($attribute, 'parent', $this); |
26
|
|
|
$this->attributes[$attribute->getName()] = $attribute; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param AttributeNode $attribute |
31
|
|
|
* @return void |
32
|
|
|
* @throw \InvalidArgumentException |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function removeAttribute(AttributeNode $attribute) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$name = $attribute->getName(); |
37
|
|
|
if (!isset($this->attributes[$name])) { |
38
|
|
|
throw new \InvalidArgumentException("Unknown node!"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
Document::setProperty($attribute, 'parent', null); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AttributeNode $node |
46
|
|
|
* @return void |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
protected function checkAttribute(AttributeNode $node) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
if (null === $node->getName()) { |
52
|
|
|
throw new \Exception("Use the createAttributeNode on document!"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (null === $node->getDocument() || $this->getDocument() !== $node->getDocument()) { |
56
|
|
|
throw new \Exception("Use the createAttributeNode on document!"); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param AttributeNode $node |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
protected function removeAttributeFromParent(AttributeNode $node) |
65
|
|
|
{ |
66
|
|
|
if (null !== $parent = $node->getParent()) { |
67
|
|
|
/** @var ObjectNode $parent */ |
68
|
|
|
$parent->removeAttribute($node); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return AttributeNode[] |
74
|
|
|
*/ |
75
|
|
|
public function getAttributes() |
76
|
|
|
{ |
77
|
|
|
return $this->attributes; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $name |
82
|
|
|
* @return AttributeNode|null |
83
|
|
|
*/ |
84
|
|
|
public function getAttribute($name) |
85
|
|
|
{ |
86
|
|
|
return isset($this->attributes[$name]) ? $this->attributes[$name] : null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param AbstractElement $node |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function addNode(AbstractElement $node) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$name = $node->getName(); |
96
|
|
|
if (isset($this->nodes[$name])) { |
97
|
|
|
throw new \InvalidArgumentException("There is allready a node with this name!"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->checkNode($node); |
101
|
|
|
$this->removeNodeFromParent($node); |
102
|
|
|
Document::setProperty($node, 'parent', $this); |
103
|
|
|
$this->nodes[$node->getName()] = $node; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param AbstractElement $node |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function removeNode(AbstractElement $node) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$name = $node->getName(); |
113
|
|
|
if (!isset($this->nodes[$name])) { |
114
|
|
|
throw new \InvalidArgumentException("Unknown node!"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
Document::setProperty($node, 'parent', null); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.