Completed
Push — issue/265 ( f8d18d )
by Alex
01:45
created

Description::setProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
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
     */
21 11
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
22
    {
23 11
        $string = '';
24 11
        foreach ($element->childNodes as $childNode) {
25 11
            if ($childNode->nodeType == XML_CDATA_SECTION_NODE) {
26 2
                $string .= $childNode->textContent;
27
            } else {
28 11
                $string .= $element->ownerDocument->saveXML($childNode);
29
            }
30
        }
31
32 11
        $node->setDescription(htmlspecialchars_decode($string));
33 11
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 5
    protected function hasValue(NodeInterface $node) : bool
39
    {
40 5
        return !! $node->getDescription();
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 5
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
47
    {
48 5
        $description = htmlspecialchars($node->getDescription());
49 5
        $element = $document->createElement($this->getNodeName(), $description);
50 5
        if ($description !== $node->getDescription() && $this->getNodeName() != 'description') {
51
            $element->setAttribute('type', 'html');
52
        }
53
54 5
        $rootElement->appendChild($element);
55 5
    }
56
}
57