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
|
98 |
|
public function __construct(string $nodeName = null) |
28
|
|
|
{ |
29
|
98 |
|
$this->nodeName = is_null($nodeName) ? static::NODE_NAME : $nodeName; |
30
|
98 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
59 |
|
public function getNodeName() : string |
36
|
|
|
{ |
37
|
59 |
|
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
|
|
|
* @return string|null |
58
|
|
|
*/ |
59
|
4 |
|
public function getChildValue(\DOMElement $element, string $name) : ? string |
60
|
|
|
{ |
61
|
4 |
|
$list = $element->getElementsByTagName($name); |
62
|
4 |
|
if ($list->length > 0) { |
63
|
3 |
|
return $list->item(0)->nodeValue; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* adds the accurate DomElement content according to the node's property |
71
|
|
|
* |
72
|
|
|
* @param \DomDocument $document |
73
|
|
|
* @param \DOMElement $rootElement |
74
|
|
|
* @param NodeInterface $node |
75
|
|
|
*/ |
76
|
25 |
|
public function apply(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
77
|
|
|
{ |
78
|
25 |
|
if ($this->hasValue($node)) { |
79
|
23 |
|
$this->addElement($document, $rootElement, $node); |
80
|
|
|
} |
81
|
25 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the attribute only if the value is not empty |
85
|
|
|
* @param DomElement $element |
86
|
|
|
* @param string $name |
87
|
|
|
* @param string $value |
88
|
|
|
*/ |
89
|
3 |
|
protected function setNonEmptyAttribute(\DomElement $element, string $name, string $value = null) : void |
90
|
|
|
{ |
91
|
3 |
|
if (! is_null($value)) { |
92
|
3 |
|
$element->setAttribute($name, $value); |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Appends a child node only if the value is not null |
98
|
|
|
* @param DomDocument $document |
99
|
|
|
* @param DOMElement $element |
100
|
|
|
* @param string $name |
101
|
|
|
* @param string $value |
102
|
|
|
*/ |
103
|
1 |
|
protected function appendNonEmptyChild(\DomDocument $document, \DOMElement $element, string $name, string $value = null) : void |
104
|
|
|
{ |
105
|
1 |
|
if (! is_null($value)) { |
106
|
1 |
|
$element->appendChild($document->createElement($name, $value)); |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the accurate $item property according to the DomElement content |
112
|
|
|
* |
113
|
|
|
* @param NodeInterface $node |
114
|
|
|
* @param \DOMElement $element |
115
|
|
|
*/ |
116
|
|
|
abstract public function setProperty(NodeInterface $node, \DOMElement $element) : void; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Tells if the node contains the expected value |
120
|
|
|
* |
121
|
|
|
* @param NodeInterface $node |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
abstract protected function hasValue(NodeInterface $node) : bool; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Creates and adds the element(s) to the document's rootElement |
128
|
|
|
* |
129
|
|
|
* @param \DomDocument $document |
130
|
|
|
* @param \DOMElement $rootElement |
131
|
|
|
* @param NodeInterface $node |
132
|
|
|
*/ |
133
|
|
|
abstract protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void; |
134
|
|
|
} |
135
|
|
|
|