Bag::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace JobQueue\Domain\Utils;
4
5
abstract class Bag implements \Countable, \SeekableIterator, \Serializable, \JsonSerializable
6
{
7
    /**
8
     *
9
     * @var array
10
     */
11
    protected $data = [];
12
13
    /**
14
     *
15
     * @return int
16
     */
17
    public function count(): int
18
    {
19
        return count($this->data);
20
    }
21
22
23
    /**
24
     *
25
     * @param int $key
26
     * @return int
27
     */
28
    public function seek($key)
29
    {
30
        if ($key >= count($this->data)) {
31
            throw new \OutOfBoundsException;
32
        }
33
34
        $this->rewind();
35
        for ($i=0; $i<$key; $i++) {
36
            $this->next();
37
        }
38
    }
39
40
    /**
41
     *
42
     * @return mixed
43
     */
44
    public function current()
45
    {
46
        return current($this->data);
47
    }
48
49
    /**
50
     *
51
     * @return mixed
52
     */
53
    public function next()
54
    {
55
        return next($this->data);
56
    }
57
58
    /**
59
     *
60
     * @return mixed
61
     */
62
    public function key()
63
    {
64
        return key($this->data);
65
    }
66
67
    /**
68
     *
69
     * @return bool
70
     */
71
    public function valid(): bool
72
    {
73
        return null !== key($this->data);
74
    }
75
76
    /**
77
     *
78
     * @return mixed
79
     */
80
    public function rewind()
81
    {
82
        return reset($this->data);
83
    }
84
85
    /**
86
     *
87
     * @return string
88
     */
89
    public function serialize(): string
90
    {
91
        return serialize($this->data);
92
    }
93
94
    /**
95
     *
96
     * @param string $serialized
97
     */
98
    public function unserialize($serialized)
99
    {
100
        $this->data = unserialize($serialized);
101
    }
102
103
    /**
104
     *
105
     * @return array
106
     */
107
    public function jsonSerialize(): array
108
    {
109
        return $this->data;
110
    }
111
112
    /**
113
     *
114
     * @return array
115
     */
116 18
    public function __toArray(): array
117
    {
118 18
        return $this->data;
119
    }
120
}
121