CallbackIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\Adapter\Transaction\Iterator;
11
12
/**
13
 * Iterator implementation with callback support.
14
 */
15
class CallbackIterator implements \Iterator
16
{
17
    /**
18
     * @var callback Callback for returning current element
19
     */
20
    protected $current;
21
22
    /**
23
     * @var callback Callback for validating next element
24
     */
25
    protected $valid;
26
27
    /**
28
     * @var int Iteration
29
     */
30
    protected $iteration = 0;
31
32
    /**
33
     * @param callback $valid   Callback for validating next element
34
     * @param callback $current Callback for returning current element
35
     */
36 2
    public function __construct($valid, $current)
37
    {
38 2
        $this->valid = $valid;
39 2
        $this->current = $current;
40 2
    }
41
42
    /**
43
     * Moves internal pointer to next position.
44
     */
45 2
    public function next()
46
    {
47 2
        ++$this->iteration;
48 2
    }
49
50
    /**
51
     * Returns current element.
52
     *
53
     * @return mixed Current element
54
     */
55 2
    public function current()
56
    {
57 2
        return call_user_func_array($this->current, array($this));
58
    }
59
60
    /**
61
     * Returns current position.
62
     *
63
     * @return int Current position
64
     */
65 2
    public function key()
66
    {
67 2
        return $this->iteration;
68
    }
69
70
    /**
71
     * Tests, if next element is valid.
72
     *
73
     * @return bool True, if valid
74
     */
75 2
    public function valid()
76
    {
77 2
        return (bool) call_user_func_array($this->valid, array($this));
78
    }
79
80
    /**
81
     * Sets internal pointer to initial position.
82
     */
83 2
    public function rewind()
84
    {
85 2
        $this->iteration = 0;
86 2
    }
87
}
88