Completed
Push — master ( 2132b9...0efe7a )
by Roberto
02:52
created

AbstractCollection::getLastIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2019 - present
4
 * updown - AbstractCollection.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 15/2/2019
8
 * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\UpDown\Abstracts;
12
13
/**
14
 * Class AbstractCollection
15
 * @package Biscolab\UpDown\Abstracts
16
 */
17
abstract class AbstractCollection
18
{
19
20
    /**
21
     * @var array
22
     */
23
    protected $items = [];
24
25
    /**
26
     * @var int
27
     */
28
    protected $index = 0;
29
30
    /**
31
     * @var string
32
     */
33
    protected $children_class = null;
34
35
    /**
36
     * AbstractCollection constructor.
37
     *
38
     * @param null|array $items
39
     */
40
    public function __construct(?array $items = [])
41
    {
42
43
        $this->setItems($items);
44
    }
45
46
    /**
47
     * @param array $items
48
     *
49
     * @return AbstractCollection
50
     */
51
    protected function setItems(?array $items = [])
52
    {
53
54
        if (is_array($items) && count($items)) {
55
            foreach ($items as $item) {
56
                $this->addItem($item);
57
            }
58
        }
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param $item
65
     *
66
     * @return AbstractCollection
67
     */
68
    public function addItem($item)
69
    {
70
71
        $item = $this->parseItem($item);
72
        array_push($this->items, $item);
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param $item
79
     *
80
     * @return mixed
81
     */
82
    protected function parseItem($item)
83
    {
84
85
        if($this->children_class && !$item instanceof $this->children_class) {
86
            $item = new $this->children_class($item);
87
        }
88
        return $item;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function toJson(): string
95
    {
96
97
        return json_encode($this->toArray());
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function toArray(): array
104
    {
105
106
        return $this->items;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function __toString(): string
113
    {
114
115
        return implode(',', $this->toArray());
116
    }
117
118
    /**
119
     * Return the current position of the index
120
     *
121
     * @return int
122
     */
123
    public function position(): int
124
    {
125
126
        return $this->index;
127
    }
128
129
    /**
130
     * Return the current object
131
     *
132
     * @return mixed|null
133
     */
134
    public function current()
135
    {
136
137
        return $this->get($this->index);
138
    }
139
140
    /**
141
     * @param $index
142
     *
143
     * @return mixed|null
144
     */
145
    public function get(int $index)
146
    {
147
148
        return isset($this->items[$index]) ? $this->items[$index] : null;
149
    }
150
151
    /**
152
     * Move index to first position and return current element
153
     *
154
     * @return mixed|null
155
     */
156
    public function first()
157
    {
158
159
        return $this->seek();
160
    }
161
162
    /**
163
     * Move the index at the specified position
164
     *
165
     * @param int|null $index
166
     *
167
     * @return mixed|null
168
     */
169
    public function seek(?int $index = 0)
170
    {
171
172
        $this->index = ($index < $this->count()) ? $index : $this->getLastIndex();
173
174
        return $this->get(intval($this->index));
175
    }
176
177
    /**
178
     * @return int
179
     */
180
    public function count(): int
181
    {
182
183
        return count($this->items);
184
    }
185
186
    /**
187
     * @return int
188
     */
189
    public function getLastIndex(): int
190
    {
191
192
        $last_position = $this->count() - 1;
193
194
        return ($last_position) < 0 ? 0 : $last_position;
195
    }
196
197
    /**
198
     * Move index at the end of collection and return current element
199
     *
200
     * @return mixed|null
201
     */
202
    public function last()
203
    {
204
205
        return $this->seek($this->getLastIndex());
206
    }
207
208
}