Completed
Pull Request — master (#257)
by Éloi
02:08
created

RuleAbstract::setProperty()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo;
12
13
use FeedIo\Feed\NodeInterface;
14
15
abstract class RuleAbstract
16
{
17
    const NODE_NAME = 'node';
18
19
    /**
20
     * @var string
21
     */
22
    protected $nodeName;
23
24
    /**
25
     * @param string $nodeName
26
     */
27 151
    public function __construct(string $nodeName = null)
28
    {
29 151
        $this->nodeName = is_null($nodeName) ? static::NODE_NAME : $nodeName;
30 151
    }
31
32
    /**
33
     * @return string
34
     */
35 108
    public function getNodeName() : string
36
    {
37 108
        return $this->nodeName;
38
    }
39
40
    /**
41
     * @param  \DOMElement $element
42
     * @param  string      $name
43
     * @return string|null
44
     */
45 59
    public function getAttributeValue(\DOMElement $element, $name) : ? string
46
    {
47 59
        if ($element->hasAttribute($name)) {
48 58
            return $element->getAttribute($name);
49
        }
50
51 49
        return null;
52
    }
53
54
    /**
55
     * @param  \DOMElement $element
56
     * @param  string      $name
57
     * @param  string      $ns
58
     * @return string|null
59
     */
60 8
    public function getChildValue(\DOMElement $element, string $name, string $ns = "") : ? string
61
    {
62 8
        $list = $element->getElementsByTagNameNS($ns, $name);
63 8
        if ($list->length > 0) {
64 4
            return $list->item(0)->nodeValue;
65
        }
66
67 4
        return null;
68
    }
69
70
    /**
71
     * @param  \DOMElement $element
72
     * @param  string      $child_name
73
     * @param  string      $attribute_name
74
     * @param  string      $ns
75
     * @return string|null
76
     */
77 3
    public function getChildAttributeValue(\DOMElement $element, string $child_name, string $attribute_name, string $ns = "") : ? string
78
    {
79 3
        $list = $element->getElementsByTagNameNS($ns, $child_name);
80 3
        if ($list->length > 0) {
81 2
            return $list->item(0)->getAttribute($attribute_name);
82
        }
83
84 1
        return null;
85
    }
86
87
88
    /**
89
     * adds the accurate DomElement content according to the node's property
90
     *
91
     * @param \DomDocument $document
92
     * @param \DOMElement $rootElement
93
     * @param NodeInterface $node
94
     */
95 28
    public function apply(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
96
    {
97 28
        if ($this->hasValue($node)) {
98 26
            $this->addElement($document, $rootElement, $node);
99
        }
100 28
    }
101
102
    /**
103
     * Sets the attribute only if the value is not empty
104
     * @param DomElement $element
105
     * @param string     $name
106
     * @param string     $value
107
     */
108 3
    protected function setNonEmptyAttribute(\DomElement $element, string $name, string $value = null) : void
109
    {
110 3
        if (! is_null($value)) {
111 3
            $element->setAttribute($name, $value);
112
        }
113 3
    }
114
115
    /**
116
     * Appends a child node only if the value is not null
117
     * @param DomDocument $document
118
     * @param DOMElement  $element
119
     * @param string      $name
120
     * @param string      $value
121
     */
122 2
    protected function appendNonEmptyChild(\DomDocument $document, \DOMElement $element, string $name, string $value = null) : void
123
    {
124 2
        if (! is_null($value)) {
125 2
            $element->appendChild($document->createElement($name, $value));
126
        }
127 2
    }
128
129
    /**
130
     * Sets the accurate $item property according to the DomElement content
131
     *
132
     * @param  NodeInterface $node
133
     * @param  \DOMElement   $element
134
     */
135
    abstract public function setProperty(NodeInterface $node, \DOMElement $element) : void;
136
137
    /**
138
     * Tells if the node contains the expected value
139
     *
140
     * @param NodeInterface $node
141
     * @return bool
142
     */
143
    abstract protected function hasValue(NodeInterface $node) : bool;
144
145
    /**
146
     * Creates and adds the element(s) to the document's rootElement
147
     *
148
     * @param \DomDocument $document
149
     * @param \DOMElement $rootElement
150
     * @param NodeInterface $node
151
     */
152
    abstract protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void;
153
}
154