1 | <?php |
||||||
2 | |||||||
3 | namespace generators; |
||||||
4 | |||||||
5 | use generators\SimpleXMLElementDecorator; |
||||||
6 | |||||||
7 | /** |
||||||
8 | * Decorator for SimpleXmlElement library with additional functionality |
||||||
9 | * @todo: rewrite to support DOMDocument instead of SimpleXmlElement |
||||||
10 | */ |
||||||
11 | final class XML extends SimpleXMLElementDecorator |
||||||
12 | { |
||||||
13 | /** |
||||||
14 | * Delete linked SimpleXmlElement node when unset XML instance |
||||||
15 | */ |
||||||
16 | function remove() |
||||||
0 ignored issues
–
show
|
|||||||
17 | { |
||||||
18 | unset($this->getSelf()[0]); |
||||||
19 | } |
||||||
20 | |||||||
21 | /** |
||||||
22 | * Add attribute to xml element |
||||||
23 | * If already exists - then update value |
||||||
24 | * @param string $name - name of the attribute |
||||||
25 | * @param string $value - full value to set |
||||||
26 | * @return XML |
||||||
27 | */ |
||||||
28 | public function setAttribute(string $name, string $value): XML |
||||||
29 | { |
||||||
30 | $attribute = $this->attributes()->{$name}; |
||||||
0 ignored issues
–
show
The method
attributes() does not exist on generators\XML . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
31 | |||||||
32 | if ($attribute) { |
||||||
33 | $this->attributes()->{$name} = $value; |
||||||
34 | } else { |
||||||
35 | $this->addAttribute($name, $value); |
||||||
0 ignored issues
–
show
The method
addAttribute() does not exist on generators\XML . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
36 | } |
||||||
37 | |||||||
38 | return $this; |
||||||
39 | } |
||||||
40 | |||||||
41 | /** |
||||||
42 | * Get attribute value by name |
||||||
43 | * @param string $name - name of the attribute |
||||||
44 | * @return string |
||||||
45 | */ |
||||||
46 | public function getAttributeValue(string $name): string |
||||||
47 | { |
||||||
48 | $attribute = $this->attributes()->{$name}; |
||||||
49 | |||||||
50 | return $attribute; |
||||||
51 | } |
||||||
52 | |||||||
53 | /** |
||||||
54 | * Find element by name and replace it with new one |
||||||
55 | * @param string $name - name of the element |
||||||
56 | * @param string $value - content of the element |
||||||
57 | * @return XML |
||||||
58 | */ |
||||||
59 | public function setUniqueChild(string $name, string $value): XML |
||||||
60 | { |
||||||
61 | $element = $this->getSingleElement($name); |
||||||
62 | |||||||
63 | if (!$element) { |
||||||
64 | |||||||
65 | return new XML($this->addChild($name, $value)); |
||||||
0 ignored issues
–
show
The method
addChild() does not exist on generators\XML . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
66 | } else { |
||||||
67 | ($element->getSelf())[0] = $value; |
||||||
68 | |||||||
69 | return $element; |
||||||
0 ignored issues
–
show
|
|||||||
70 | } |
||||||
71 | } |
||||||
72 | |||||||
73 | /** |
||||||
74 | * @return array<XML>|null |
||||||
75 | */ |
||||||
76 | public function getSingleElement(string $name, string $id = '') |
||||||
77 | { |
||||||
78 | $elements = $this->getElements($name, $id); |
||||||
79 | return $elements ? $elements[0] : null; |
||||||
80 | } |
||||||
81 | /** |
||||||
82 | * Get elements by name or by name and id |
||||||
83 | * |
||||||
84 | * @param string $name - name of elements to find, 'item' for <item> |
||||||
85 | * @param string $id - id of elements to find, 'main' for <item id="main"> |
||||||
86 | * |
||||||
87 | * @return array of XML elements |
||||||
88 | */ |
||||||
89 | public function getElements(string $name, string $id = '') |
||||||
90 | { |
||||||
91 | $elements = $this->xpath('descendant::' . $name . ($id ? '[@id="' . $id . '"]' : '')); |
||||||
0 ignored issues
–
show
The method
xpath() does not exist on generators\XML . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
92 | |||||||
93 | return array_map(function($element) { |
||||||
94 | return new XML($element); |
||||||
95 | }, $elements); |
||||||
96 | } |
||||||
97 | |||||||
98 | public function getSelf() |
||||||
99 | { |
||||||
100 | return $this->simpleXMLElement[0]; |
||||||
101 | } |
||||||
102 | } |
||||||
103 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.