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\Rule; |
12
|
|
|
|
13
|
|
|
use FeedIo\Feed\NodeInterface; |
14
|
|
|
use FeedIo\RuleAbstract; |
15
|
|
|
use FeedIo\RuleSet; |
16
|
|
|
|
17
|
|
|
class Structure extends RuleAbstract |
18
|
|
|
{ |
19
|
|
|
const NODE_NAME = 'structure'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \FeedIo\RuleSet |
23
|
|
|
*/ |
24
|
|
|
protected $ruleSet; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $nodeName |
28
|
|
|
* @param RuleSet $ruleSet |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct(string $nodeName = null, RuleSet $ruleSet = null) |
31
|
|
|
{ |
32
|
6 |
|
parent::__construct($nodeName); |
33
|
|
|
|
34
|
6 |
|
$this->ruleSet = is_null($ruleSet) ? new RuleSet() : $ruleSet; |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param NodeInterface $node |
39
|
|
|
* @param \DOMElement $element |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
2 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) : void |
43
|
|
|
{ |
44
|
2 |
|
foreach ($element->childNodes as $domNode) { |
45
|
2 |
|
if ($domNode instanceof \DomElement) { |
46
|
2 |
|
$rule = $this->ruleSet->get($domNode->tagName); |
47
|
2 |
|
$rule->setProperty($node, $domNode); |
48
|
|
|
} |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
1 |
|
protected function hasValue(NodeInterface $node) : bool |
56
|
|
|
{ |
57
|
1 |
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
1 |
View Code Duplication |
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
66
|
|
|
|
67
|
|
|
/** @var RuleAbstract $rule */ |
68
|
1 |
|
foreach ($this->ruleSet->getRules() as $rule) { |
69
|
1 |
|
$rule->apply($document, $element, $node); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$rootElement->appendChild($element); |
73
|
1 |
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.