Completed
Push — master ( 0570a2...e72b03 )
by Maxim
02:24
created

Routes   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A key() 0 3 1
A offsetSet() 0 3 1
A current() 0 3 1
A getData() 0 3 1
A next() 0 3 1
A rewind() 0 3 1
A offsetGet() 0 3 1
B addRoute() 0 16 5
A valid() 0 3 1
A offsetExists() 0 3 1
A offsetUnset() 0 3 1
1
<?php
2
3
namespace WebComplete\mvc\router;
4
5
class Routes implements \ArrayAccess, \Iterator
6
{
7
8
    /**
9
     * @var array
10
     */
11
    private $config;
12
    private $position = 0;
13
14
    /**
15
     * @param array $config
16
     */
17
    public function __construct(array $config)
18
    {
19
        $this->config = $config;
20
    }
21
22
    /**
23
     * @return array
24
     */
25
    public function getData(): array
26
    {
27
        return $this->config;
28
    }
29
30
    /**
31
     * @param mixed $offset
32
     *
33
     * @return bool
34
     */
35
    public function offsetExists($offset): bool
36
    {
37
        return isset($this->config[$offset]);
38
    }
39
40
    /**
41
     * @param mixed $offset
42
     *
43
     * @return mixed|null
44
     */
45
    public function offsetGet($offset)
46
    {
47
        return $this->config[$offset] ?? null;
48
    }
49
50
    /**
51
     * @param mixed $offset
52
     * @param mixed $value
53
     */
54
    public function offsetSet($offset, $value)
55
    {
56
        $this->config[$offset] = $value;
57
    }
58
59
    /**
60
     * @param mixed $offset
61
     */
62
    public function offsetUnset($offset)
63
    {
64
        unset($this->config[$offset]);
65
    }
66
67
    /**
68
     * Return the current element
69
     * @return array
70
     */
71
    public function current()
72
    {
73
        return $this->config[$this->position];
74
    }
75
76
    /**
77
     * Move forward to next element
78
     */
79
    public function next()
80
    {
81
        $this->position++;
82
    }
83
84
    /**
85
     * Return the key of the current element
86
     * @return int
87
     */
88
    public function key()
89
    {
90
        return $this->position;
91
    }
92
93
    /**
94
     * Checks if current position is valid
95
     * @return bool
96
     */
97
    public function valid()
98
    {
99
        return isset($this->config[$this->position]);
100
    }
101
102
    /**
103
     * Rewind the Iterator to the first element
104
     */
105
    public function rewind()
106
    {
107
        $this->position = 0;
108
    }
109
110
    /**
111
     * @param array $routeDefinition
112
     * @param string|null $beforeRoute
113
     */
114
    public function addRoute(array $routeDefinition, string $beforeRoute = null)
115
    {
116
        if ($beforeRoute) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $beforeRoute of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117
            /**
118
             * @var int $k
119
             * @var array $route
120
             */
121
            foreach ((array)$this->config as $k => $route) {
122
                if ($route[1] === $beforeRoute) {
123
                    $k > 1
124
                        ? \array_splice($this->config, $k-1, 0, $routeDefinition)
125
                        : \array_unshift($this->config, $routeDefinition);
126
                }
127
            }
128
        } else {
129
            \array_unshift($this->config, $routeDefinition);
130
        }
131
    }
132
}
133