Completed
Push — project/php-7 ( 101bb9 )
by Alex
01:53
created

OptionalField::addElementsFromNodeList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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