Completed
Push — master ( 5fca22...233410 )
by Emily
01:43
created

Transformation::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection\Transformation;
16
17
use OuterIterator;
18
use Spaark\CompositeUtils\Model\Collection\IterableIterator;
19
use Spaark\CompositeUtils\Model\Collection\OuterIteratorTrait;
20
21
/**
22
 * A Transformation represents an iterator which iterates over something
23
 * iterable and transforms it in some way
24
 *
25
 * Notable and common examples are: maps, filters and splicing
26
 */
27
abstract class Transformation implements OuterIterator
28
{
29
    use OuterIteratorTrait;
30
31
    /**
32
     * The inner iterator being transformed
33
     *
34
     * @var IterableIterator
35
     * @construct required
36
     */
37
    protected $iterator;
38
39
    /**
40
     * The current key
41
     *
42
     * @var scalar
43
     */
44
    protected $key;
45
46
    /**
47
     * The current value
48
     *
49
     * @var mixed
50
     */
51
    protected $current;
52
53
    /**
54
     * Constructs the Transformation with the iterable original data
55
     */
56 1
    public function __construct(iterable $iterator)
57
    {
58 1
        $this->iterator = new IterableIterator($iterator);
59 1
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 1
    public function rewind()
65
    {
66 1
        $this->iterator->rewind();
67 1
        $this->doProcess();
68 1
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function next()
74
    {
75 1
        $this->iterator->next();
76 1
        $this->doProcess();
77 1
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function current()
83
    {
84 1
        return $this->current;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 1
    public function key()
91
    {
92 1
        return $this->key;
93
    }
94
95
    /**
96
     * Calls the child class' process() method if and only if this is
97
     * valid()
98
     */
99 1
    private function doProcess()
100
    {
101 1
        if ($this->valid())
102
        {
103 1
            $this->process();
104
        }
105 1
    }
106
107
    /**
108
     * Called on whenever the pointer is moved (on rewind() or next()
109
     * calls) to process the current position place
110
     */
111
    abstract protected function process();
112
}
113