Completed
Push — ruudvdd-fetch-subtags-issue-76 ( 94f122 )
by Alex
03:52
created

OptionalField   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 119
ccs 47
cts 48
cp 0.9792
rs 10

7 Methods

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