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

Category::addElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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 2
eloc 3
nc 2
nop 3
crap 2
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\Node\CategoryInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\RuleAbstract;
16
17
class Category extends RuleAbstract
18
{
19
    const NODE_NAME = 'category';
20
        
21
    /**
22
     * @param  NodeInterface $node
23
     * @param  \DOMElement   $element
24
     * @return mixed
25
     */
26 2
    public function setProperty(NodeInterface $node, \DOMElement $element)
27
    {
28 2
        $category = $node->newCategory();
29 2
        $category->setScheme($this->getAttributeValue($element, 'domain'))
30 2
        ->setLabel($element->nodeValue)
31 2
        ->setTerm($element->nodeValue);
32 2
        $node->addCategory($category);
33
34 2
        return $this;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40 4
    protected function hasValue(NodeInterface $node) : bool
41
    {
42 4
        return !! $node->getCategories();
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 4
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
49
    {
50 4
        foreach ($node->getCategories() as $category) {
51 4
            $rootElement->appendChild($this->createCategoryElement($document, $category));
52
        }
53 4
    }
54
55
    /**
56
     * @param  \DomDocument   $document
57
     * @param  CategoryInterface $category
58
     * @return \DomElement
59
     */
60 4
    public function createCategoryElement(\DomDocument $document, CategoryInterface $category)
61
    {
62 4
        $element = $document->createElement(
63 4
            $this->getNodeName(),
64 4
            is_null($category->getTerm()) ? $category->getLabel():$category->getTerm()
65
            );
66 4
        if (!! $category->getScheme()) {
67 2
            $element->setAttribute('domain', $category->getScheme());
68
        }
69
70 4
        return $element;
71
    }
72
}
73