Completed
Pull Request — 3.x (#127)
by Joschi
02:04
created

RuleIterator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 138
c 2
b 0
f 0
ccs 39
cts 39
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A append() 0 4 1
A prepend() 0 4 1
A current() 0 20 4
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A set() 0 7 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Router\Rule;
10
11
use Aura\Router\Exception;
12
use Iterator;
13
14
/**
15
 *
16
 * Collection of rules to iterate through.
17
 *
18
 * @package Aura.Router
19
 *
20
 */
21
class RuleIterator implements Iterator
22
{
23
    /**
24
     *
25
     * The rules to iterate through.
26
     *
27
     * @var array
28
     *
29
     */
30
    protected $rules = [];
31
32
    /**
33
     *
34
     * Constructor.
35
     *
36
     * @param array $rules The rules to iterate through.
37
     *
38
     */
39 8
    public function __construct(array $rules = [])
40
    {
41 8
        $this->set($rules);
42 8
    }
43
44
    /**
45
     *
46
     * Sets the rules to iterate through.
47
     *
48
     * @param array $rules The rules to iterate through.
49
     *
50
     */
51 8
    public function set(array $rules)
52
    {
53 8
        $this->rules = [];
54 8
        foreach ($rules as $rule) {
55 8
            $this->append($rule);
56 8
        }
57 8
    }
58
59
    /**
60
     *
61
     * Appends a rule to iterate through.
62
     *
63
     * @param callable $rule The rule to iterate through.
64
     *
65
     */
66 8
    public function append(callable $rule)
67
    {
68 8
        $this->rules[] = $rule;
69 8
    }
70
71
    /**
72
     *
73
     * Prepends a rule to iterate through.
74
     *
75
     * @param callable $rule The rule to iterate through.
76
     *
77
     */
78 1
    public function prepend(callable $rule)
79
    {
80 1
        array_unshift($this->rules, $rule);
81 1
    }
82
83
    /**
84
     *
85
     * Iterator: gets the current rule.
86
     *
87
     * @return RuleInterface
88
     *
89
     */
90 8
    public function current()
91
    {
92 8
        $rule = current($this->rules);
93 8
        if ($rule instanceof RuleInterface) {
94 6
            return $rule;
95
        }
96
97 3
        $key = key($this->rules);
98 3
        $factory = $this->rules[$key];
99 3
        $rule = $factory();
100 3
        if ($rule instanceof RuleInterface) {
101 1
            $this->rules[$key] = $rule;
102 1
            return $rule;
103
        }
104
105 2
        $message = gettype($rule);
106 2
        $message .= ($message != 'object') ?: ' of type ' . get_class($rule);
107 2
        $message = "Expected RuleInterface, got {$message} for key {$key}";
108 2
        throw new Exception\UnexpectedValue($message);
109
    }
110
111
    /**
112
     *
113
     * Iterator: gets the current rule key.
114
     *
115
     * @return mixed
116
     *
117
     */
118 1
    public function key()
119
    {
120 1
        return key($this->rules);
121
    }
122
123
    /**
124
     *
125
     * Iterator: moves the iterator forward to the next rule.
126
     *
127
     * @return null
128
     *
129
     */
130 6
    public function next()
131
    {
132 6
        next($this->rules);
133 6
    }
134
135
    /**
136
     *
137
     * Iterator: rewinds to the first rule.
138
     *
139
     * @return null
140
     *
141
     */
142 6
    public function rewind()
143
    {
144 6
        reset($this->rules);
145 6
    }
146
147
    /**
148
     *
149
     * Iterator: is the current position valid?
150
     *
151
     * @return bool
152
     *
153
     */
154 6
    public function valid()
155
    {
156 6
        return current($this->rules) !== false;
157
    }
158
}
159