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 | 151 | 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 $ns |
||
58 | * @return string|null |
||
59 | */ |
||
60 | 8 | public function getChildValue(\DOMElement $element, string $name, string $ns = "") : ? string |
|
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 |
|
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 |
|
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 |
|
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 |