CollectionIterator::key()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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