Completed
Push — master ( 605ec5...e9cc16 )
by Vitaly
02:18
created

AbstractIterable::rewind()   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 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 08.04.17 at 09:13
5
 */
6
namespace samsonframework\stringconditiontree;
7
8
use Countable;
9
use Iterator;
10
11
/**
12
 * Class GenericIterable
13
 *
14
 * @author Vitaly Egorov <[email protected]>
15
 */
16
abstract class AbstractIterable implements Iterator, Countable
17
{
18
    /** string Internal collection name for iteration and counting */
19
    protected const COLLECTION_NAME = 'internalCollection';
20
21
    /** @var array Internal internalCollection storage */
22
    private $internalCollection;
23
24
    /**
25
     * GenericIterable constructor.
26
     *
27
     * @param string $collectionName Collection variable nested class name
28
     */
29 22
    public function __construct(string $collectionName = self::COLLECTION_NAME)
30
    {
31
        // Set pointer for internal iterable and countable collection to passed property by its name
32 22
        $this->internalCollection = &$this->$collectionName;
33
34
        // Set empty array
35 22
        $this->internalCollection = [];
36 22
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41 4
    public function current()
42
    {
43 4
        return current($this->internalCollection);
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 4
    public function next()
50
    {
51 4
        next($this->internalCollection);
52 4
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 5
    public function valid()
58
    {
59 5
        $key = $this->key();
60
61 5
        return ($key !== null && $key !== false);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 5
    public function key()
68
    {
69 5
        return key($this->internalCollection);
70
    }
71
72
    /**
73
     * Rewind the Iterator to the first element
74
     * @link  http://php.net/manual/en/iterator.rewind.php
75
     * @return void Any returned value is ignored.
76
     * @since 5.0.0
77
     */
78 5
    public function rewind()
79
    {
80 5
        reset($this->internalCollection);
81 5
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 3
    public function count()
87
    {
88 3
        return count($this->internalCollection);
89
    }
90
}
91