Completed
Pull Request — master (#79)
by
unknown
03:16
created

OptionalField   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 110
ccs 45
cts 46
cp 0.9783
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 8 1
B addSubElements() 0 16 5
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
        $childNodeList = $domNode->childNodes;
49 2
        foreach ($childNodeList as $childNode) {
50 2
            if ($childNode instanceof \DOMText) {
51
                continue;
52
            }
53
54 2
            $element->addElement($this->createElementFromDomNode($node, $childNode));
55 2
        }
56 2
    }
57
58
    /**
59
     * @param \DOMNode $domNode
60
     * @return bool
61
     */
62 9
    private function hasSubElements(\DOMNode $domNode)
63
    {
64 9
        foreach ($domNode->childNodes as $childDomNode) {
65 9
            if (!$childDomNode instanceof \DOMText) {
66 2
                return true;
67
            }
68 8
        }
69
70 8
        return false;
71
    }
72
73
    /**
74
     * @param NodeInterface $node
75
     * @param \DOMNode $domNode
76
     * @return ElementInterface
77
     */
78 9
    private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode)
79
    {
80 9
        $element = $node->newElement();
81 9
        $element->setName($domNode->nodeName);
82 9
        $element->setValue($domNode->nodeValue);
83
84 9
        foreach ($domNode->attributes as $attribute) {
85 3
            $element->setAttribute($attribute->name, $attribute->value);
86 9
        }
87 9
        $this->addSubElements($node, $element, $domNode);
88
89 9
        return $element;
90
    }
91
92
    /**
93
     * creates the accurate DomElement content according to the $item's property
94
     *
95
     * @param  \DomDocument  $document
96
     * @param  NodeInterface $node
97
     * @return \DomElement
98
     */
99 6
    public function createElement(\DomDocument $document, NodeInterface $node)
100
    {
101 6
        $domElement = $document->createElement($this->getNodeName());
102 6
        foreach ($node->getElementIterator($this->getNodeName()) as $element) {
103 5
            $this->buildDomElement($domElement, $element);
104 6
        }
105
106 6
        return $domElement;
107
    }
108
109 5
    public function buildDomElement(\DomElement $domElement, ElementInterface $element)
110
    {
111 5
        $domElement->nodeValue = $element->getValue();
112
113 5
        foreach ($element->getAttributes() as $name => $value) {
114 1
            $domElement->setAttribute($name, $value);
115 5
        }
116
117
        /** @var ElementInterface $subElement */
118 5
        foreach ($element->getAllElements() as $subElement) {
119 1
            $subDomElement = $domElement->ownerDocument->createElement($subElement->getName());
120 1
            $this->buildDomElement($subDomElement, $subElement);
121 1
            $domElement->appendChild($subDomElement);
122 5
        }
123
124 5
        return $domElement;
125
    }
126
}
127