DataCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 138
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A addItem() 0 12 2
A getItem() 0 4 1
A count() 0 4 1
A removeItem() 0 12 2
1
<?php
2
/*
3
 * This file is part of the php-utilities package.
4
 *
5
 * (c) Marc Aschmann <[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
namespace Asm\Data;
11
12
/**
13
 * Class DataCollection
14
 *
15
 * @package Asm\Data
16
 * @author Marc Aschmann <[email protected]>
17
 */
18
final class DataCollection implements DataCollectionInterface
19
{
20
    use DataTrait;
21
22
    const STORAGE_KEY = 'items';
23
24
    /**
25
     * @var int
26
     */
27
    private $position;
28
29
    /**
30
     * @param array $data
31
     */
32
    public function __construct(array $data)
33
    {
34
        if (null !== $data && is_array($data)) {
35
            $this->set(self::STORAGE_KEY, $data);
36
        }
37
38
        $this->position = 0;
39
    }
40
41
    /**
42
     * (PHP 5 >= 5.0.0)<br/>
43
     * Return the current element
44
     * @link http://php.net/manual/en/iterator.current.php
45
     * @return mixed Can return any type.
46
     */
47
    public function current()
48
    {
49
        return $this->get(self::STORAGE_KEY, $this->position);
50
    }
51
52
    /**
53
     * (PHP 5 >= 5.0.0)<br/>
54
     * Move forward to next element
55
     * @link http://php.net/manual/en/iterator.next.php
56
     * @return void Any returned value is ignored.
57
     */
58
    public function next()
59
    {
60
        ++$this->position;
61
    }
62
63
    /**
64
     * (PHP 5 >= 5.0.0)<br/>
65
     * Return the key of the current element
66
     * @link http://php.net/manual/en/iterator.key.php
67
     * @return int scalar on success, or null on failure.
68
     */
69
    public function key()
70
    {
71
        return $this->position;
72
    }
73
74
    /**
75
     * (PHP 5 >= 5.0.0)<br/>
76
     * Checks if current position is valid
77
     * @link http://php.net/manual/en/iterator.valid.php
78
     * @return bool The return value will be casted to bool and then evaluated.
79
     *                 Returns true on success or false on failure.
80
     */
81
    public function valid() : bool
82
    {
83
        return (bool)$this->get(self::STORAGE_KEY, $this->position, false);
84
    }
85
86
    /**
87
     * (PHP 5 >= 5.0.0)<br/>
88
     * Rewind the Iterator to the first element
89
     * @link http://php.net/manual/en/iterator.rewind.php
90
     * @return void Any returned value is ignored.
91
     */
92
    public function rewind()
93
    {
94
        $this->position = 0;
95
    }
96
97
    /**
98
     * Add element to store.
99
     *
100
     * @param  mixed $item
101
     * @param  int $position
102
     * @return $this
103
     */
104
    public function addItem($item, $position = null)
105
    {
106
        if (null === $position) {
107
            $items = $this->get(self::STORAGE_KEY, []);
108
            array_push($items, $item);
109
            $this->set(self::STORAGE_KEY, $items);
110
        } else {
111
            $this->set(self::STORAGE_KEY, $position, $item);
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * Get element from store.
119
     *
120
     * @param  int $position
121
     * @return bool|mixed
122
     */
123
    public function getItem(int $position = 0)
124
    {
125
        return $this->get(self::STORAGE_KEY, $position, false);
126
    }
127
128
    /**
129
     * @return int
130
     */
131
    public function count() : int
132
    {
133
        return count($this->get(self::STORAGE_KEY, []));
134
    }
135
136
    /**
137
     * remove item from position
138
     * be carefull: this also reindexes the array
139
     *
140
     * @param  int $position
141
     * @return $this
142
     */
143
    public function removeItem(int $position)
144
    {
145
        $items = $this->get(self::STORAGE_KEY, []);
146
147
        if (isset($items[$position])) {
148
            unset($items[$position]);
149
            $this->set(self::STORAGE_KEY, null);
150
            $this->set(self::STORAGE_KEY, array_values($items));
151
        }
152
153
        return $this;
154
    }
155
}
156