1 | <?php |
||
12 | class Element extends \DOMElement implements |
||
13 | NodeInterface, |
||
14 | NodeLocatorInterface |
||
15 | { |
||
16 | use EscaperAwareTrait; |
||
17 | use LocatorAwareTrait; |
||
18 | |||
19 | /** |
||
20 | * Return an element collection |
||
21 | * |
||
22 | * @param string|array $locator |
||
23 | * @return NodeList |
||
24 | */ |
||
25 | public function locate($locator) |
||
30 | |||
31 | /** |
||
32 | * @param string $value Node value. |
||
33 | * @return $this |
||
34 | */ |
||
35 | public function setValue($value) |
||
40 | |||
41 | /** |
||
42 | * @param string $name |
||
43 | * @param string $value |
||
44 | * @return $this |
||
45 | */ |
||
46 | public function setAttribute($name, $value) |
||
56 | |||
57 | /** |
||
58 | * @param string|array $html Valid XHTML code. |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function setHtml($html) |
||
62 | { |
||
63 | $html = $this->normalizeHtml($html); |
||
64 | if (empty($html)) { |
||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | $this->nodeValue = ''; |
||
69 | $frag = $this->ownerDocument->createDocumentFragment(); |
||
70 | $frag->appendXml($html); |
||
71 | |||
72 | // TODO refactor |
||
73 | if ($frag->firstChild) { |
||
74 | $this->appendChild($frag); |
||
75 | } else { |
||
76 | /** @var Element $node */ |
||
77 | foreach ((new Dom($html))->locate('body') as $node) { |
||
78 | foreach ($node->childNodes as $child) { |
||
79 | $this->appendChild($this->ownerDocument->importNode($child, true)); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns the node body html |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getInnerHtml() |
||
101 | |||
102 | /** |
||
103 | * Returns the node html |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getOuterHtml() |
||
111 | |||
112 | /** |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function isEmpty() |
||
132 | |||
133 | /** |
||
134 | * @param string $nodeName New node name. |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function rename($nodeName) |
||
154 | |||
155 | /** |
||
156 | * @param string|array $html |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function replace($html) |
||
160 | { |
||
161 | $html = $this->normalizeHtml($html); |
||
162 | if (empty($html)) { |
||
163 | $this->remove(); |
||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | $frag = $this->ownerDocument->createDocumentFragment(); |
||
168 | $frag->appendXML($html); |
||
169 | |||
170 | // TODO refactor |
||
171 | if ($frag->firstChild) { |
||
172 | $node = $this->parentNode->insertBefore($frag, $this); |
||
173 | } else { |
||
174 | $nodes = []; |
||
175 | /** @var Element $node */ |
||
176 | foreach ((new Dom($html))->locate('body') as $subNode) { |
||
177 | foreach ($subNode->childNodes as $child) { |
||
178 | // TODO add node to node list |
||
179 | $nodes[] = $this->parentNode->insertBefore($this->ownerDocument->importNode($child, true), $this); |
||
180 | } |
||
181 | } |
||
182 | $node = new NodeList($nodes); |
||
183 | } |
||
184 | |||
185 | $this->remove(); |
||
186 | return $node; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function remove() |
||
197 | |||
198 | /** |
||
199 | * @param string|array $html |
||
200 | * @return string |
||
201 | */ |
||
202 | private function normalizeHtml($html) |
||
206 | } |
||
207 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.