Completed
Push — master ( 54cc6d...ed66d0 )
by Alex
11s
created

Category::setNonEmptyAttribute()   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
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
crap 2
1
<?php declare(strict_types=1);
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\Atom;
12
13
use FeedIo\Feed\Node\CategoryInterface;
14
use FeedIo\Feed\NodeInterface;
15
16
class Category extends \FeedIo\Rule\Category
17
{
18
19
    /**
20
     * @param  NodeInterface $node
21
     * @param  \DOMElement   $element
22
     */
23 3
    public function setProperty(NodeInterface $node, \DOMElement $element) : void
24
    {
25 3
        $category = $node->newCategory();
26 3
        $category->setScheme($this->getAttributeValue($element, 'scheme'))
27 3
        ->setLabel($this->getAttributeValue($element, 'label'))
28 3
        ->setTerm($this->getAttributeValue($element, 'term'));
29
30 3
        $node->addCategory($category);
31 3
    }
32
33
    /**
34
     * @param  \DomDocument   $document
35
     * @param  CategoryInterface $category
36
     * @return \DomElement
37
     */
38 3
    public function createCategoryElement(\DomDocument $document, CategoryInterface $category) : \DOMElement
39
    {
40 3
        $element = $document->createElement($this->getNodeName());
41 3
        $this->setNonEmptyAttribute($element, 'scheme', $category->getScheme());
42 3
        $this->setNonEmptyAttribute($element, 'term', $category->getTerm());
43 3
        $this->setNonEmptyAttribute($element, 'label', $category->getLabel());
44
45 3
        return $element;
46
    }
47
48
    /**
49
     * Sets the attribute only if the value is not emtpy
50
     * @param DomElement $element
51
     * @param string     $name
52
     * @param string     $value
53
     */
54 3
    protected function setNonEmptyAttribute(\DomElement $element, string $name, string $value = null) : void
55
    {
56 3
        if (! is_null($value)) {
57 3
            $element->setAttribute($name, $value);
58
        }
59 3
    }
60
}
61