|
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
|
104 |
|
public function __construct(string $nodeName = null) |
|
28
|
|
|
{ |
|
29
|
104 |
|
$this->nodeName = is_null($nodeName) ? static::NODE_NAME : $nodeName; |
|
30
|
104 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
62 |
|
public function getNodeName() : string |
|
36
|
|
|
{ |
|
37
|
62 |
|
return $this->nodeName; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \DOMElement $element |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* @return string|null |
|
44
|
|
|
*/ |
|
45
|
12 |
|
public function getAttributeValue(\DOMElement $element, $name) : ? string |
|
46
|
|
|
{ |
|
47
|
12 |
|
if ($element->hasAttribute($name)) { |
|
48
|
11 |
|
return $element->getAttribute($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \DOMElement $element |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @param string $ns |
|
58
|
|
|
* @return string|null |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function getChildValue(\DOMElement $element, string $name, string $ns = "") : ? string |
|
61
|
|
|
{ |
|
62
|
5 |
|
$list = $element->getElementsByTagNameNS($ns, $name); |
|
63
|
5 |
|
if ($list->length > 0) { |
|
64
|
3 |
|
return $list->item(0)->nodeValue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
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
|
1 |
|
public function getChildAttributeValue(\DOMElement $element, string $child_name, string $attribute_name, string $ns = "") : ? string |
|
78
|
|
|
{ |
|
79
|
1 |
|
$list = $element->getElementsByTagNameNS($ns, $child_name); |
|
80
|
1 |
|
if ($list->length > 0) { |
|
81
|
1 |
|
return $list->item(0)->getAttribute($attribute_name); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
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
|
|
|
|