Completed
Push — issue/84 ( 0f4f84 )
by Alex
13s
created

RuleAbstract::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
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;
12
13
use FeedIo\Feed\NodeInterface;
14
15
abstract class RuleAbstract
16
{
17
    const NODE_NAME = 'node';
18
19
    /**
20
     * @var string
21
     */
22
    protected $nodeName;
23
24
    /**
25
     * @param string $nodeName
26
     */
27 90
    public function __construct($nodeName = null)
28
    {
29 90
        $this->nodeName = is_null($nodeName) ? static::NODE_NAME : $nodeName;
30 90
    }
31
32
    /**
33
     * @return string
34
     */
35 52
    public function getNodeName()
36
    {
37 52
        return $this->nodeName;
38
    }
39
40
    /**
41
     * @param  \DOMElement $element
42
     * @param  string      $name
43
     * @return string|null
44
     */
45 8
    public function getAttributeValue(\DOMElement $element, $name)
46
    {
47 8
        if ($element->hasAttribute($name)) {
48 7
            return $element->getAttribute($name);
49
        }
50
51 3
        return;
52
    }
53
54
    /**
55
     * @param  \DOMElement $element
56
     * @param  string      $name
57
     * @return string|null
58
     */
59 3
    public function getChildValue(\DOMElement $element, $name)
60
    {
61 3
        $list = $element->getElementsByTagName($name);
62 3
        if ( $list->length > 0 ) {
63 3
            return $list->item(0)->nodeValue;
64
        }
65
66
        return;
67
    }
68
69
    /**
70
     * Sets the accurate $item property according to the DomElement content
71
     *
72
     * @param  NodeInterface $node
73
     * @param  \DOMElement   $element
74
     * @return mixed
75
     */
76
    abstract public function setProperty(NodeInterface $node, \DOMElement $element);
77
78
    /**
79
     * creates the accurate DomElement content according to the $item's property
80
     *
81
     * @param  \DomDocument  $document
82
     * @param  NodeInterface $node
83
     * @return \DomElement
84
     */
85
    abstract public function createElement(\DomDocument $document, NodeInterface $node);
86
}
87