1 | <?php |
||
24 | class XMLReader |
||
25 | { |
||
26 | /** |
||
27 | * DOMDocument object |
||
28 | * |
||
29 | * @var \DOMDocument |
||
30 | */ |
||
31 | private $dom = null; |
||
32 | |||
33 | /** |
||
34 | * DOMXpath object |
||
35 | * |
||
36 | * @var \DOMXpath |
||
37 | */ |
||
38 | private $xpath = null; |
||
39 | |||
40 | /** |
||
41 | * Get DOMDocument from ZipArchive |
||
42 | * |
||
43 | * @param string $zipFile |
||
44 | * @param string $xmlFile |
||
45 | * @return \DOMDocument|false |
||
46 | * @throws \Exception |
||
47 | */ |
||
48 | 2 | public function getDomFromZip($zipFile, $xmlFile) |
|
65 | |||
66 | /** |
||
67 | * Get DOMDocument from content string |
||
68 | * |
||
69 | * @param string $content |
||
70 | * @return \DOMDocument |
||
71 | */ |
||
72 | 6 | public function getDomFromString($content) |
|
73 | { |
||
74 | 6 | $originalLibXMLEntityValue = libxml_disable_entity_loader(true); |
|
75 | 6 | $this->dom = new \DOMDocument(); |
|
76 | 6 | $this->dom->loadXML($content); |
|
77 | 6 | libxml_disable_entity_loader($originalLibXMLEntityValue); |
|
78 | |||
79 | 6 | return $this->dom; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get elements |
||
84 | * |
||
85 | * @param string $path |
||
86 | * @param \DOMElement $contextNode |
||
87 | * @return \DOMNodeList |
||
88 | */ |
||
89 | 6 | public function getElements($path, \DOMElement $contextNode = null) |
|
104 | |||
105 | /** |
||
106 | * Registers the namespace with the DOMXPath object |
||
107 | * |
||
108 | * @param string $prefix The prefix |
||
109 | * @param string $namespaceURI The URI of the namespace |
||
110 | * @return bool true on success or false on failure |
||
111 | * @throws \InvalidArgumentException If called before having loaded the DOM document |
||
112 | */ |
||
113 | 2 | public function registerNamespace($prefix, $namespaceURI) |
|
123 | |||
124 | /** |
||
125 | * Get element |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param \DOMElement $contextNode |
||
129 | * @return \DOMElement|null |
||
130 | */ |
||
131 | 3 | public function getElement($path, \DOMElement $contextNode = null) |
|
140 | |||
141 | /** |
||
142 | * Get element attribute |
||
143 | * |
||
144 | * @param string $attribute |
||
145 | * @param \DOMElement $contextNode |
||
146 | * @param string $path |
||
147 | * @return string|null |
||
148 | */ |
||
149 | 1 | public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) |
|
167 | |||
168 | /** |
||
169 | * Get element value |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param \DOMElement $contextNode |
||
173 | * @return string|null |
||
174 | */ |
||
175 | 2 | public function getValue($path, \DOMElement $contextNode = null) |
|
184 | |||
185 | /** |
||
186 | * Count elements |
||
187 | * |
||
188 | * @param string $path |
||
189 | * @param \DOMElement $contextNode |
||
190 | * @return integer |
||
191 | */ |
||
192 | 1 | public function countElements($path, \DOMElement $contextNode = null) |
|
198 | |||
199 | /** |
||
200 | * Element exists |
||
201 | * |
||
202 | * @param string $path |
||
203 | * @param \DOMElement $contextNode |
||
204 | * @return boolean |
||
205 | */ |
||
206 | 4 | public function elementExists($path, \DOMElement $contextNode = null) |
|
210 | } |
||
211 |
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.