1 | <?php |
||
18 | class Node |
||
19 | { |
||
20 | /** |
||
21 | * Node document. |
||
22 | * |
||
23 | * @var DOMDocument |
||
24 | */ |
||
25 | protected $document; |
||
26 | |||
27 | /** |
||
28 | * Node element. |
||
29 | * |
||
30 | * @var DOMElement |
||
31 | */ |
||
32 | protected $element; |
||
33 | |||
34 | /** |
||
35 | * Define the parent node name. |
||
36 | * |
||
37 | * @var string|null |
||
38 | */ |
||
39 | protected $parentNodeName = null; |
||
40 | |||
41 | /** |
||
42 | * Define the node name. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $nodeName; |
||
47 | |||
48 | /** |
||
49 | * Node attributes. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $attr = []; |
||
54 | |||
55 | /** |
||
56 | * Create a new node instance. |
||
57 | * |
||
58 | * @param array $attr |
||
59 | */ |
||
60 | public function __construct(array ...$attr) |
||
74 | |||
75 | /** |
||
76 | * Add a new node. |
||
77 | * |
||
78 | * @param \Kinedu\CfdiXML\Common\Node $node |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function add(Node $node) |
||
138 | |||
139 | /** |
||
140 | * Search the direct child of an element. |
||
141 | * |
||
142 | * @param DOMNodeList $children |
||
143 | * @param string $find |
||
144 | * |
||
145 | * @return DOMElement|null |
||
146 | */ |
||
147 | protected function getDirectChildElementByName(DOMNodeList $children, string $find) |
||
157 | |||
158 | /** |
||
159 | * @param \Kinedu\CfdiXML\Common\Node $node |
||
160 | * @return void |
||
161 | */ |
||
162 | public function setNamespace(Node $node) |
||
188 | |||
189 | /** |
||
190 | * @param string $schemaDefinition |
||
191 | * @return void |
||
192 | */ |
||
193 | public function setSchemaDefinition(string $schemaDefinition) |
||
206 | |||
207 | /** |
||
208 | * Get node attributes. |
||
209 | * |
||
210 | * @param string $index |
||
211 | * |
||
212 | * @return array|null |
||
213 | */ |
||
214 | public function getAttr(string $index = 'node') |
||
224 | |||
225 | /** |
||
226 | * Adds attributes to an element. |
||
227 | * |
||
228 | * @param DOMElement $element |
||
229 | * @param array $attr |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | public function setAttr(DOMElement $element, array $attr = null) |
||
241 | |||
242 | /** |
||
243 | * Get element. |
||
244 | * |
||
245 | * @return DOMElement |
||
246 | */ |
||
247 | public function getElement(): DOMElement |
||
251 | |||
252 | /** |
||
253 | * Get document. |
||
254 | * |
||
255 | * @return DOMDocument |
||
256 | */ |
||
257 | public function getDocument(): DOMDocument |
||
261 | |||
262 | /** |
||
263 | * Get wrapper node name. |
||
264 | * |
||
265 | * @return string|null |
||
266 | */ |
||
267 | public function getWrapperNodeName() |
||
271 | |||
272 | /** |
||
273 | * Get parent node name. |
||
274 | * |
||
275 | * @return string|null |
||
276 | */ |
||
277 | public function getParentNodeName() |
||
281 | |||
282 | /** |
||
283 | * Get node name. |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getNodeName() |
||
291 | |||
292 | public function hasSchemaDefinition(Node $node): bool |
||
296 | |||
297 | public function isGlobalDefinition(Node $node): bool |
||
301 | } |
||
302 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: