Completed
Push — master ( 4c7da2...64af60 )
by Alex
02:34 queued 46s
created

OptionalField::createElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
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\ElementsAwareInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\Feed\Node\ElementInterface;
16
use FeedIo\RuleAbstract;
17
18
class OptionalField extends RuleAbstract
19
{
20
    const NODE_NAME = 'default';
21
22
    /**
23
     * @param  NodeInterface $node
24
     * @param  \DOMElement   $domElement
25
     */
26 9
    public function setProperty(NodeInterface $node, \DOMElement $domElement) : void
27
    {
28 9
        $element = $this->createElementFromDomNode($node, $domElement);
29
30 9
        $node->addElement($element);
31 9
    }
32
33
    /**
34
     * @param NodeInterface $node
35
     * @param ElementInterface $element
36
     * @param \DOMNode $domNode
37
     */
38 9
    private function addSubElements(NodeInterface $node, ElementInterface $element, \DOMNode $domNode) : void
39
    {
40 9
        if (!$domNode->hasChildNodes() || !$this->hasSubElements($domNode)) {
41
            // no elements to add
42 9
            return;
43
        }
44
45 2
        $this->addElementsFromNodeList($node, $element, $domNode->childNodes);
46 2
    }
47
48
    /**
49
     * @param NodeInterface $node
50
     * @param ElementInterface $element
51
     * @param \DOMNodeList $childNodeList
52
     */
53 2
    private function addElementsFromNodeList(NodeInterface $node, ElementInterface $element, \DOMNodeList $childNodeList) : void
54
    {
55 2
        foreach ($childNodeList as $childNode) {
56 2
            if ($childNode instanceof \DOMText) {
57
                continue;
58
            }
59
60 2
            $element->addElement($this->createElementFromDomNode($node, $childNode));
61
        }
62 2
    }
63
64
    /**
65
     * @param \DOMNode $domNode
66
     * @return bool
67
     */
68 9
    private function hasSubElements(\DOMNode $domNode) : bool
69
    {
70 9
        foreach ($domNode->childNodes as $childDomNode) {
71 9
            if (!$childDomNode instanceof \DOMText) {
72 9
                return true;
73
            }
74
        }
75
76 8
        return false;
77
    }
78
79
    /**
80
     * @param NodeInterface $node
81
     * @param \DOMNode $domNode
82
     * @return ElementInterface
83
     */
84 9
    private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode) : ElementInterface
85
    {
86 9
        $element = $node->newElement();
87 9
        $element->setName($domNode->nodeName);
88 9
        $element->setValue($domNode->nodeValue);
89
90 9
        foreach ($domNode->attributes as $attribute) {
91 3
            $element->setAttribute($attribute->name, $attribute->value);
92
        }
93 9
        $this->addSubElements($node, $element, $domNode);
94
95 9
        return $element;
96
    }
97
98
    /**
99
     * @param \DomElement $domElement
100
     * @param ElementInterface $element
101
     * @return \DomElement
102
     */
103 5
    public function buildDomElement(\DomElement $domElement, ElementInterface $element) : \DOMElement
104
    {
105 5
        $domElement->nodeValue = $element->getValue();
106
107 5
        foreach ($element->getAttributes() as $name => $value) {
108 1
            $domElement->setAttribute($name, $value);
109
        }
110
111
        /** @var ElementInterface $subElement */
112 5
        foreach ($element->getAllElements() as $subElement) {
113 1
            $subDomElement = $domElement->ownerDocument->createElement($subElement->getName());
114 1
            $this->buildDomElement($subDomElement, $subElement);
115 1
            $domElement->appendChild($subDomElement);
116
        }
117
118 5
        return $domElement;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124 6
    protected function hasValue(NodeInterface $node) : bool
125
    {
126 6
        return $node instanceof ElementsAwareInterface;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 6 View Code Duplication
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
133
    {
134 6
        $domElement = $document->createElement($this->getNodeName());
135 6
        if ($node instanceof ElementsAwareInterface) {
136 6
            foreach ($node->getElementIterator($this->getNodeName()) as $element) {
137 5
                $this->buildDomElement($domElement, $element);
138
            }
139
        }
140
141 6
        $rootElement->appendChild($domElement);
142 6
    }
143
}
144