Completed
Push — master ( 8931e3...83677e )
by
unknown
02:02
created

CollectionIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Lan\Ebs\Sdk\Classes;
4
5
use ArrayIterator;
6
7
class CollectionIterator extends ArrayIterator
8
{
9
    /**
10
     * @var Collection
11
     */
12
    private $collection;
13
14
    private $data = [];
15
16
    /**
17
     * CollectionIterator constructor.
18
     *
19
     * @param Collection $collection
20
     */
21 2
    public function __construct(Collection $collection)
22
    {
23 2
        $this->collection = $collection;
24 2
    }
25
26
    /**
27
     * Return the current element
28
     *
29
     * @link   http://php.net/manual/en/iterator.current.php
30
     * @return Model
31
     * @throws \Exception
32
     */
33 1
    public function current()
34
    {
35 1
        return $this->collection->createModel(current($this->data));
36
    }
37
38
    /**
39
     * Move forward to next element
40
     *
41
     * @link   http://php.net/manual/en/iterator.next.php
42
     * @return Model
43
     */
44 1
    public function next()
45
    {
46 1
        return next($this->data);
47
    }
48
49
    /**
50
     * Checks if current position is valid
51
     *
52
     * @link   http://php.net/manual/en/iterator.valid.php
53
     * @return boolean
54
     */
55 2
    public function valid()
56
    {
57 2
        $key = $this->key();
58
59 2
        return ($key !== null && $key !== false);
60
    }
61
62
    /**
63
     * Return the key of the current element
64
     *
65
     * @link   http://php.net/manual/en/iterator.key.php
66
     * @return integer
67
     */
68 2
    public function key()
69
    {
70 2
        return key($this->data);
71
    }
72
73
    /**
74
     * Rewind the Iterator to the first element
75
     *
76
     * @link   http://php.net/manual/en/iterator.rewind.php
77
     * @return void
78
     * @throws \Exception
79
     */
80 2
    public function rewind()
81
    {
82 2
        $this->data = $this->collection->load()->getData();
83
84 2
        reset($this->data);
85
    }
86
}