Issues (43)

app/generators/Xml.php (6 issues)

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
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ignore-call  annotation

30
        $attribute = $this->/** @scrutinizer ignore-call */ attributes()->{$name};
Loading history...
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 ignore-call  annotation

35
            $this->/** @scrutinizer ignore-call */ 
36
                   addAttribute($name, $value);
Loading history...
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 ignore-call  annotation

65
            return new XML($this->/** @scrutinizer ignore-call */ addChild($name, $value));
Loading history...
66
        } else {
67
            ($element->getSelf())[0] = $value;
68
69
            return $element;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $element returns the type generators\XML[] which is incompatible with the type-hinted return generators\XML.
Loading history...
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 ignore-call  annotation

91
        /** @scrutinizer ignore-call */ 
92
        $elements = $this->xpath('descendant::' . $name . ($id ? '[@id="' . $id . '"]' : ''));
Loading history...
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