Completed
Push — master ( 6e4f15...3feaf0 )
by Alex
20s queued 11s
created

Description::processString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 12
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
22
    {
23 12
        $string = '';
24 12
        foreach ($element->childNodes as $childNode) {
25 12
            if ($childNode->nodeType == XML_CDATA_SECTION_NODE) {
26
                $string .= $childNode->textContent;
27
            } else {
28 12
                $string .= $element->ownerDocument->saveXML($childNode);
29
            }
30
        }
31
32 12
        $node->setDescription($this->processString($string, $node));
33 12
    }
34
35
    /**
36
     * @param string $input
37
     * @param NodeInterface $node
38
     * @return string
39
     */
40 12
    protected function processString(string $input, NodeInterface $node): string
41
    {
42 12
        return str_replace(
43 12
            ['href="/', 'src="/'],
44 12
            ["href=\"{$node->getHost()}/", "src=\"{$node->getHost()}/"],
45 12
            htmlspecialchars_decode($input)
46
        );
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 5
    protected function hasValue(NodeInterface $node) : bool
53
    {
54 5
        return !! $node->getDescription();
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 5
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
61
    {
62 5
        $description = htmlspecialchars($node->getDescription());
63 5
        $element = $document->createElement($this->getNodeName(), $description);
64 5
        if ($description !== $node->getDescription() && $this->getNodeName() != 'description') {
65
            $element->setAttribute('type', 'html');
66
        }
67
68 5
        $rootElement->appendChild($element);
69 5
    }
70
}
71