CollectionIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 88
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A current() 0 4 1
A next() 0 4 1
A valid() 0 6 2
A key() 0 4 1
A rewind() 0 6 1
1
<?php
2
/**
3
 * Class CollectionIterator
4
 *
5
 * @author       Denis Shestakov <[email protected]>
6
 * @copyright    Copyright (c) 2017, Lan Publishing
7
 * @license      MIT
8
 */
9
10
namespace Lan\Ebs\Sdk\Classes;
11
12
use ArrayIterator;
13
14
/**
15
 * Итератор коллекции
16
 *
17
 * @package      Lan\Ebs
18
 * @subpackage   Sdk
19
 * @category     Classes
20
 */
21
class CollectionIterator extends ArrayIterator
22
{
23
    /**
24
     * Инстанс коллекции
25
     *
26
     * @var Collection
27
     */
28
    private $collection;
29
30
31
    /**
32
     * Данные коллекции
33
     *
34
     * @var array
35
     */
36
    private $data = array();
37
38
    /**
39
     * Конструктор итератора
40
     *
41
     * @param Collection $collection Коллекция
42
     */
43
    public function __construct(Collection $collection)
44
    {
45
        $this->collection = $collection;
46
    }
47
48
    /**
49
     * Return the current element
50
     *
51
     * @link   http://php.net/manual/en/iterator.current.php
52
     * @return Model
53
     * @throws \Exception
54
     */
55
    public function current()
56
    {
57
        return $this->collection->createModel(current($this->data));
58
    }
59
60
    /**
61
     * Move forward to next element
62
     *
63
     * @link   http://php.net/manual/en/iterator.next.php
64
     * @return Model
65
     */
66
    public function next()
67
    {
68
        return next($this->data);
69
    }
70
71
    /**
72
     * Checks if current position is valid
73
     *
74
     * @link   http://php.net/manual/en/iterator.valid.php
75
     * @return boolean
76
     */
77
    public function valid()
78
    {
79
        $key = $this->key();
80
81
        return ($key !== null && $key !== false);
82
    }
83
84
    /**
85
     * Return the key of the current element
86
     *
87
     * @link   http://php.net/manual/en/iterator.key.php
88
     * @return integer
89
     */
90
    public function key()
91
    {
92
        return key($this->data);
93
    }
94
95
    /**
96
     * Rewind the Iterator to the first element
97
     *
98
     * @link   http://php.net/manual/en/iterator.rewind.php
99
     * @return void
100
     * @throws \Exception
101
     */
102
    public function rewind()
103
    {
104
        $this->data = $this->collection->load()->getData();
105
106
        reset($this->data);
107
    }
108
}