Feed::newItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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