Completed
Push — ruudvdd-fetch-subtags-issue-76 ( 94f122 )
by Alex
03:52
created

ElementsAwareTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 100
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initElements() 0 4 1
A newElement() 0 4 1
A getValue() 0 8 2
A getElementIterator() 0 4 1
A hasElement() 0 6 1
A addElement() 0 6 1
A getAllElements() 0 4 1
A listElements() 0 6 2
A getElementsGenerator() 0 8 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\Feed;
12
13
use FeedIo\Feed\Node\Element;
14
use FeedIo\Feed\Node\ElementInterface;
15
use FeedIo\Feed\Node\ElementIterator;
16
17
trait ElementsAwareTrait
18
{
19
    /**
20
     * @var \ArrayIterator
21
     */
22
    protected $elements;
23
24
    /**
25
     * initialize the elements property before use
26
     */
27 98
    protected function initElements()
28
    {
29 98
        $this->elements = new \ArrayIterator();
30 98
    }
31
32
    /**
33
     * @return ElementInterface
34
     */
35 21
    public function newElement()
36
    {
37 21
        return new Element();
38
    }
39
40
    /**
41
     * @param  string $name element name
42
     * @return mixed
43
     */
44 4
    public function getValue($name)
45
    {
46 4
        foreach ($this->getElementIterator($name) as $element) {
47 4
            return $element->getValue();
48 1
        }
49
50 1
        return;
51
    }
52
53
    /**
54
     * @param  string $name element name
55
     * @return ElementIterator
56
     */
57 16
    public function getElementIterator($name)
58
    {
59 16
        return new ElementIterator($this->elements, $name);
60
    }
61
62
    /**
63
     * @param  string $name element name
64
     * @return boolean true if the element exists
65
     */
66 4
    public function hasElement($name)
67
    {
68 4
        $filter = $this->getElementIterator($name);
69
70 4
        return $filter->count() > 0;
71
    }
72
73
    /**
74
     * @param  ElementInterface $element
75
     * @return $this
76
     */
77 25
    public function addElement(ElementInterface $element)
78
    {
79 25
        $this->elements->append($element);
80
81 25
        return $this;
82
    }
83
84
    /**
85
     * Returns all the item's optional elements
86
     * @return \ArrayIterator
87
     */
88 14
    public function getAllElements()
89
    {
90 14
        return $this->elements;
91
    }
92
93
    /**
94
     * Returns the item's optional elements tag names
95
     * @return array
96
     */
97 7
    public function listElements()
98
    {
99 7
        foreach ($this->elements as $element) {
100 4
            yield ($element->getName());
101 7
        }
102 7
    }
103
104
    /**
105
     * @return \Generator
106
     */
107 4
    public function getElementsGenerator()
108
    {
109 4
        $elements = $this->getAllElements();
110
111 4
        foreach ($elements as $element) {
112 2
            yield $element->getName() => $element->getValue();
113 3
        }
114 3
    }
115
116
}
117