CollectionIterator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 11
cp 0
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A next() 0 3 1
A valid() 0 4 1
A rewind() 0 3 1
A key() 0 3 1
1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Copyright © 2015-2021 Nelson Martell (http://nelson6e65.github.io)
7
 *
8
 * Licensed under The MIT License (MIT)
9
 * For full copyright and license information, please see the LICENSE
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright 2015-2021 Nelson Martell
13
 * @link      http://nelson6e65.github.io/php_nml/
14
 * @since     0.4.0
15
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
16
 * */
17
18
namespace NelsonMartell\Collections;
19
20
/**
21
 * Implementa los métodos de la interfaz Iterator para una colección de objetos.
22
 *
23
 * @author Nelson Martell <[email protected]>
24
 * @since  0.4.0
25
 * */
26
trait CollectionIterator
27
{
28
    private $iteratorPosition = 0;
29
30
    public function current()
31
    {
32
        return $this->getItem($this->iteratorPosition);
33
    }
34
35
    public function rewind()
36
    {
37
        $this->iteratorPosition = 0;
38
    }
39
40
    public function key()
41
    {
42
        return $this->iteratorPosition;
43
    }
44
45
    public function next()
46
    {
47
        ++$this->iteratorPosition;
48
    }
49
50
    public function valid()
51
    {
52
        $v = (bool) ($this->getItem($this->iteratorPosition) != null);
53
        return $v;
54
    }
55
56
    abstract protected function getItem($index);
57
}
58