Completed
Pull Request — project/php-7 (#133)
by Alex
04:04 queued 01:49
created

Description::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 26/10/14
6
 * Time: 00:26
7
 */
8
namespace FeedIo\Rule;
9
10
use FeedIo\Feed\NodeInterface;
11
use FeedIo\RuleAbstract;
12
13
class Description extends RuleAbstract
14
{
15
    const NODE_NAME = 'description';
16
17
    /**
18
     * @param  NodeInterface $node
19
     * @param  \DOMElement   $element
20
     * @return $this
21
     */
22 7
    public function setProperty(NodeInterface $node, \DOMElement $element)
23
    {
24 7
        $string = '';
25 7
        if ($element->firstChild && $element->firstChild->nodeType == XML_CDATA_SECTION_NODE) {
26
            $string = $element->firstChild->textContent;
27
        } else {
28 7
            foreach ($element->childNodes as $childNode) {
29 7
                $string .= $element->ownerDocument->saveXML($childNode);
30
            }
31
        }
32
33 7
        $node->setDescription(htmlspecialchars_decode($string));
34
35 7
        return $this;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 3
    protected function hasValue(NodeInterface $node) : bool
42
    {
43 3
        return !! $node->getDescription();
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 3
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
50
    {
51 3
        $description = htmlspecialchars($node->getDescription());
52
53 3
        $rootElement->appendChild($document->createElement($this->getNodeName(), $description));
54 3
    }
55
}
56