Completed
Pull Request — master (#158)
by Alex
06:05 queued 02:56
created

Description::setProperty()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

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