1 | <?php declare(strict_types=1); |
||
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 | 152 | public function __construct(string $nodeName = null) |
|
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | 108 | public function getNodeName() : string |
|
39 | |||
40 | /** |
||
41 | * @param \DOMElement $element |
||
42 | * @param string $name |
||
43 | * @return string|null |
||
44 | */ |
||
45 | 59 | public function getAttributeValue(\DOMElement $element, $name) : ? string |
|
53 | |||
54 | /** |
||
55 | * @param \DOMElement $element |
||
56 | * @param string $name |
||
57 | * @param string|null $ns |
||
58 | * @return string|null |
||
59 | */ |
||
60 | 9 | public function getChildValue(\DOMElement $element, string $name, ?string $ns = null) : ? string |
|
61 | { |
||
62 | 9 | if ($ns === null) { |
|
63 | 7 | $list = $element->getElementsByTagName($name); |
|
64 | } else { |
||
65 | 3 | $list = $element->getElementsByTagNameNS($ns, $name); |
|
66 | } |
||
67 | 9 | if ($list->length > 0) { |
|
68 | 7 | return $list->item(0)->nodeValue; |
|
69 | } |
||
70 | |||
71 | 3 | return null; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param \DOMElement $element |
||
76 | * @param string $child_name |
||
77 | * @param string $attribute_name |
||
78 | * @param string|null $ns |
||
79 | * @return string|null |
||
80 | */ |
||
81 | 3 | public function getChildAttributeValue(\DOMElement $element, string $child_name, string $attribute_name, ?string $ns = null) : ? string |
|
82 | { |
||
83 | 3 | if ($ns === null) { |
|
84 | $list = $element->getElementsByTagName($child_name); |
||
85 | } else { |
||
86 | 3 | $list = $element->getElementsByTagNameNS($ns, $child_name); |
|
87 | } |
||
88 | 3 | if ($list->length > 0) { |
|
89 | 2 | return $list->item(0)->getAttribute($attribute_name); |
|
90 | } |
||
91 | |||
92 | 1 | return null; |
|
93 | } |
||
94 | |||
95 | |||
96 | /** |
||
97 | * adds the accurate DomElement content according to the node's property |
||
98 | * |
||
99 | * @param \DomDocument $document |
||
100 | * @param \DOMElement $rootElement |
||
101 | * @param NodeInterface $node |
||
102 | */ |
||
103 | 29 | public function apply(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
104 | { |
||
105 | 29 | if ($this->hasValue($node)) { |
|
106 | 27 | $this->addElement($document, $rootElement, $node); |
|
107 | } |
||
108 | 29 | } |
|
109 | |||
110 | /** |
||
111 | * Sets the attribute only if the value is not empty |
||
112 | * @param DomElement $element |
||
113 | * @param string $name |
||
114 | * @param string $value |
||
115 | */ |
||
116 | 3 | protected function setNonEmptyAttribute(\DomElement $element, string $name, string $value = null) : void |
|
117 | { |
||
118 | 3 | if (! is_null($value)) { |
|
119 | 3 | $element->setAttribute($name, $value); |
|
120 | } |
||
121 | 3 | } |
|
122 | |||
123 | /** |
||
124 | * Appends a child node only if the value is not null |
||
125 | * @param DomDocument $document |
||
126 | * @param DOMElement $element |
||
127 | * @param string $name |
||
128 | * @param string $value |
||
129 | */ |
||
130 | 2 | protected function appendNonEmptyChild(\DomDocument $document, \DOMElement $element, string $name, string $value = null) : void |
|
131 | { |
||
132 | 2 | if (! is_null($value)) { |
|
133 | 2 | $element->appendChild($document->createElement($name, $value)); |
|
134 | } |
||
135 | 2 | } |
|
136 | |||
137 | /** |
||
138 | * Sets the accurate $item property according to the DomElement content |
||
139 | * |
||
140 | * @param NodeInterface $node |
||
141 | * @param \DOMElement $element |
||
142 | */ |
||
143 | abstract public function setProperty(NodeInterface $node, \DOMElement $element) : void; |
||
144 | |||
145 | /** |
||
146 | * Tells if the node contains the expected value |
||
147 | * |
||
148 | * @param NodeInterface $node |
||
149 | * @return bool |
||
150 | */ |
||
151 | abstract protected function hasValue(NodeInterface $node) : bool; |
||
152 | |||
153 | /** |
||
154 | * Creates and adds the element(s) to the document's rootElement |
||
155 | * |
||
156 | * @param \DomDocument $document |
||
157 | * @param \DOMElement $rootElement |
||
158 | * @param NodeInterface $node |
||
159 | */ |
||
160 | abstract protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void; |
||
161 | } |
||
162 |