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
|
93 |
|
public function __construct(string $nodeName = null) |
28
|
|
|
{ |
29
|
93 |
|
$this->nodeName = is_null($nodeName) ? static::NODE_NAME : $nodeName; |
30
|
93 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
54 |
|
public function getNodeName() : string |
36
|
|
|
{ |
37
|
54 |
|
return $this->nodeName; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \DOMElement $element |
42
|
|
|
* @param string $name |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
9 |
|
public function getAttributeValue(\DOMElement $element, $name) : ? string |
46
|
|
|
{ |
47
|
9 |
|
if ($element->hasAttribute($name)) { |
48
|
8 |
|
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
|
23 |
|
public function apply(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
77
|
|
|
{ |
78
|
23 |
|
if ($this->hasValue($node)) { |
79
|
21 |
|
$this->addElement($document, $rootElement, $node); |
80
|
|
|
} |
81
|
23 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the accurate $item property according to the DomElement content |
85
|
|
|
* |
86
|
|
|
* @param NodeInterface $node |
87
|
|
|
* @param \DOMElement $element |
88
|
|
|
*/ |
89
|
|
|
abstract public function setProperty(NodeInterface $node, \DOMElement $element) : void; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Tells if the node contains the expected value |
93
|
|
|
* |
94
|
|
|
* @param NodeInterface $node |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
abstract protected function hasValue(NodeInterface $node) : bool; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Creates and adds the element(s) to the document's rootElement |
101
|
|
|
* |
102
|
|
|
* @param \DomDocument $document |
103
|
|
|
* @param \DOMElement $rootElement |
104
|
|
|
* @param NodeInterface $node |
105
|
|
|
*/ |
106
|
|
|
abstract protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void; |
107
|
|
|
} |
108
|
|
|
|