Completed
Push — master ( 5f476f...c14aad )
by Denis
01:46
created

CollectionIterator::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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