1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FluentDOM\Creator { |
4
|
|
|
|
5
|
|
|
use FluentDOM\Appendable; |
6
|
|
|
use FluentDOM\Creator; |
7
|
|
|
use FluentDOM\DOM\Document; |
8
|
|
|
use FluentDOM\DOM\Element; |
9
|
|
|
use FluentDOM\Transformer\Namespaces\Optimize; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Internal class for the FluentDOM\Creator, please do not use directly |
13
|
|
|
* |
14
|
|
|
* @property-read Document $document |
15
|
|
|
* @property-read Element $node |
16
|
|
|
*/ |
17
|
|
|
class Node implements Appendable, \IteratorAggregate { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Document |
21
|
|
|
*/ |
22
|
|
|
private $_document; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \DOMElement |
26
|
|
|
*/ |
27
|
|
|
private $_node; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Creator |
31
|
|
|
*/ |
32
|
|
|
private $_creator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Creator $creator |
36
|
|
|
* @param Document $document |
37
|
|
|
* @param \DOMElement $node |
38
|
|
|
*/ |
39
|
12 |
|
public function __construct(Creator $creator, Document $document, \DOMElement $node) { |
40
|
12 |
|
$this->_creator = $creator; |
41
|
12 |
|
$this->_document = $document; |
42
|
12 |
|
$this->_node = $node; |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function __isset(string $name) { |
50
|
|
|
switch ($name) { |
51
|
|
|
case 'document' : |
52
|
|
|
case 'node' : |
53
|
|
|
return TRUE; |
54
|
|
|
} |
55
|
|
|
return FALSE; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
7 |
|
public function __get(string $name) { |
63
|
|
|
switch ($name) { |
64
|
7 |
|
case 'document' : |
65
|
4 |
|
return $this->getDocument(); |
66
|
3 |
|
case 'node' : |
67
|
2 |
|
return $this->_node; |
68
|
|
|
} |
69
|
1 |
|
return NULL; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @param mixed $value |
75
|
|
|
* @throws \BadMethodCallException |
76
|
|
|
*/ |
77
|
1 |
|
public function __set(string $name, $value) { |
78
|
1 |
|
throw new \BadMethodCallException( |
79
|
1 |
|
sprintf('%s is immutable.', get_class($this)) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
|
|
* @throws \BadMethodCallException |
86
|
|
|
*/ |
87
|
|
|
public function __unset(string $name) { |
88
|
|
|
throw new \BadMethodCallException( |
89
|
|
|
sprintf('%s is immutable.', get_class($this)) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Document |
95
|
|
|
*/ |
96
|
7 |
|
public function getDocument(): Document { |
97
|
7 |
|
$document = clone $this->_document; |
98
|
7 |
|
$document->appendChild($document->importNode($this->_node, TRUE)); |
99
|
7 |
|
if ($this->_creator->optimizeNamespaces) { |
100
|
6 |
|
$document = (new Optimize($document))->getDocument(); |
101
|
6 |
|
$document->formatOutput = $this->_document->formatOutput; |
102
|
|
|
} |
103
|
7 |
|
return $document; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
public function __toString(): string { |
110
|
2 |
|
return $this->getDocument()->saveXML() ?: ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Element $parent |
115
|
|
|
* @return Element |
116
|
|
|
*/ |
117
|
7 |
|
public function appendTo(Element $parent): Element { |
118
|
7 |
|
$parent->appendChild( |
119
|
7 |
|
$parent->ownerDocument->importNode($this->_node, TRUE) |
120
|
|
|
); |
121
|
7 |
|
return $parent; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return \ArrayIterator |
126
|
|
|
*/ |
127
|
1 |
|
public function getIterator() { |
128
|
1 |
|
return new \ArrayIterator([$this->node]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |