Completed
Pull Request — master (#219)
by
unknown
02:09
created

Feed::addNS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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;
12
13
use FeedIo\Feed\Node;
14
use FeedIo\Feed\Item;
15
use FeedIo\Feed\ItemInterface;
16
17
class Feed extends Node implements FeedInterface, \JsonSerializable
18
{
19
    /**
20
     * @var \ArrayIterator
21
     */
22
    protected $items;
23
24
    /**
25
     * @var string $url
26
     */
27
    protected $url;
28
29
    /**
30
     * @var string $language
31
     */
32
    protected $language;
33
34
    protected $ns;
35
36 41
    public function __construct()
37
    {
38 41
        $this->items = new \ArrayIterator();
39 41
        $this->ns = new \ArrayIterator();
40
41 41
        parent::__construct();
42 41
    }
43
44
    /**
45
     * @return string $url
46
     */
47 3
    public function getUrl() : ? string
48
    {
49 3
        return $this->url;
50
    }
51
52
    /**
53
     * @param string $url
54
     * @return FeedInterface
55
     */
56 5
    public function setUrl(string $url = null) : FeedInterface
57
    {
58 5
        $this->url = $url;
59
60 5
        return $this;
61
    }
62
63
    /**
64
     * @return string $language
65
     */
66 4
    public function getLanguage(): ? string
67
    {
68 4
        return $this->language;
69
    }
70
71
    /**
72
     * @param string $language
73
     * @return FeedInterface
74
     */
75 2
    public function setLanguage(string $language = null): FeedInterface
76
    {
77 2
        $this->language = $language;
78
79 2
        return $this;
80
    }
81
82
83
84
85
    /**
86
     * (PHP 5 &gt;= 5.0.0)<br/>
87
     * Return the current element
88
     * @link http://php.net/manual/en/iterator.current.php
89
     * @return mixed Can return any type.
90
     */
91 19
    public function current()
92
    {
93 19
        return $this->items->current();
94
    }
95
96
    /**
97
     * (PHP 5 &gt;= 5.0.0)<br/>
98
     * Move forward to next element
99
     * @link http://php.net/manual/en/iterator.next.php
100
     * @return void Any returned value is ignored.
101
     */
102 17
    public function next()
103
    {
104 17
        $this->items->next();
105 17
    }
106
107
    /**
108
     * (PHP 5 &gt;= 5.0.0)<br/>
109
     * Return the key of the current element
110
     * @link http://php.net/manual/en/iterator.key.php
111
     * @return mixed scalar on success, or null on failure.
112
     */
113 2
    public function key()
114
    {
115 2
        return $this->items->key();
116
    }
117
118
    /**
119
     * (PHP 5 &gt;= 5.0.0)<br/>
120
     * Checks if current position is valid
121
     * @link http://php.net/manual/en/iterator.valid.php
122
     * @return boolean The return value will be casted to boolean and then evaluated.
123
     *                 Returns true on success or false on failure.
124
     */
125 21
    public function valid()
126
    {
127 21
        return $this->items->valid();
128
    }
129
130
    /**
131
     * (PHP 5 &gt;= 5.0.0)<br/>
132
     * Rewind the Iterator to the first element
133
     * @link http://php.net/manual/en/iterator.rewind.php
134
     * @return void Any returned value is ignored.
135
     */
136 19
    public function rewind()
137
    {
138 19
        $this->items->rewind();
139 19
    }
140
141
    /**
142
     * @param  ItemInterface $item
143
     * @return $this
144
     */
145 28
    public function add(ItemInterface $item) : FeedInterface
146
    {
147 28
        $this->items->append($item);
148
149 28
        return $this;
150
    }
151
152 2
    public function addNS(string $ns, string $dtd) : FeedInterface
153
    {
154 2
        $this->ns->offsetSet($ns, $dtd);
155
156 2
        return $this;
157
    }
158
159 6
    public function getNS() : \ArrayIterator
160
    {
161 6
        return $this->ns;
162
    }
163
164
    /**
165
     * @return ItemInterface
166
     */
167 10
    public function newItem() : ItemInterface
168
    {
169 10
        return new Item();
170
    }
171
172
    /**
173
     * @return array
174
     */
175 1
    public function jsonSerialize() : array
176
    {
177 1
        return $this->toArray();
178
    }
179
180
    /**
181
     * @return array
182
     */
183 2
    public function toArray() : array
184
    {
185 2
        $items = [];
186
187 2
        foreach ($this->items as $item) {
188 2
            $items[] = $item->toArray();
189
        }
190
191 2
        $properties = parent::toArray();
192 2
        $properties['items'] = $items;
193
194 2
        return $properties;
195
    }
196
197
    /**
198
     * @return int
199
     */
200 1
    public function count() : int
201
    {
202 1
        return count($this->items);
203
    }
204
}
205