Completed
Pull Request — master (#86)
by
unknown
02:49
created

ElementsAwareTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
dl 0
loc 87
c 4
b 0
f 2
wmc 10
lcom 1
cbo 2
ccs 26
cts 26
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initElements() 0 4 1
A newElement() 0 4 1
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 ElementIterator
43
     */
44 16
    public function getElementIterator($name)
45
    {
46 16
        return new ElementIterator($this->elements, $name);
47
    }
48
49
    /**
50
     * @param  string $name element name
51
     * @return boolean true if the element exists
52
     */
53 4
    public function hasElement($name)
54
    {
55 4
        $filter = $this->getElementIterator($name);
56
57 4
        return $filter->count() > 0;
58
    }
59
60
    /**
61
     * @param  ElementInterface $element
62
     * @return $this
63
     */
64 25
    public function addElement(ElementInterface $element)
65
    {
66 25
        $this->elements->append($element);
67
68 25
        return $this;
69
    }
70
71
    /**
72
     * Returns all the item's optional elements
73
     * @return \ArrayIterator
74
     */
75 14
    public function getAllElements()
76
    {
77 14
        return $this->elements;
78
    }
79
80
    /**
81
     * Returns the item's optional elements tag names
82
     * @return array
83
     */
84 7
    public function listElements()
85
    {
86 7
        foreach ($this->elements as $element) {
87 4
            yield ($element->getName());
88 7
        }
89 7
    }
90
91
    /**
92
     * @return \Generator
93
     */
94 4
    public function getElementsGenerator()
95
    {
96 4
        $elements = $this->getAllElements();
97
98 4
        foreach ($elements as $element) {
99 2
            yield $element->getName() => $element->getValue();
100 3
        }
101 3
    }
102
103
}
104