Route::getDefaultNamedParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @package    Fuel\Routing
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2015 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\Routing;
12
13
use LogicException;
14
15
class Route
16
{
17
	public $name;
18
	public $router;
19
	public $methods;
20
	public $resource;
21
	public $translation;
22
	public $parameters = array();
23
	public $filters = array();
24
25 1
	public function __construct(Router $router, array $methods, $resource, $translation, $name)
26
	{
27 1
		$this->name = $name;
28 1
		$this->router = $router;
29 1
		$this->methods = $methods;
30
31 1
		if (is_string($resource))
32
		{
33 1
			$this->resource = $this->parseTypes($resource);
34
		}
35
36 1
		$this->translation = $translation;
37 1
	}
38
39
	public function name($name)
40
	{
41
		$this->unregister();
42
		$this->name = $name;
43
		$this->router->inject($this);
44
45
		return $this;
46
	}
47
48
	public function unregister()
49
	{
50
		$this->router->unregister($this);
51
52
		return $this;
53
	}
54
55 1
	protected function parseTypes($resource)
56
	{
57 1
		$route = $this;
58 1
		$router = $this->router;
59
60
		$filter = function ($match) use ($route, $router)
61
		{
62
			list (, $type, $name) = $match;
63
			$type = $router->getType($type);
64
			$route->parameter($name, $type['regex'], $type['optional']);
65
66
			return '{'.$name.'}';
67 1
		};
68
69 1
		return preg_replace_callback('#\{([a-zA-Z_]+)\:([a-zA-Z_]+)\}#', $filter, $resource);
70
	}
71
72
	public function filters(array $filters)
73
	{
74
		$normalized = array();
75
76
		foreach ($filters as $filter => $parameters)
77
		{
78
			if (is_numeric($filter))
79
			{
80
				$normalized[$parameters] = [true];
81
			}
82
			else
83
			{
84
				$normalized[$filter] = (array) $parameters;
85
			}
86
		}
87
88
		$this->filters = array_merge($this->filters, $normalized);
89
90
		return $this;
91
	}
92
93
	public function filter($filter, $parameters = true)
94
	{
95
		$this->filters[$filter] = (array) $parameters;
96
97
		return $this;
98
	}
99
100
	public function parameter($parameter, $regex = '(.*)+', $optional = false)
101
	{
102
		if (is_string($parameter))
103
		{
104
			$parameter = new Parameter($this, $parameter, $regex, $optional);
105
		}
106
107
		$this->parameters[$parameter->name] = $parameter;
108
109
		return $parameter;
110
	}
111
112
	public function compileRegex()
113
	{
114
		$search = [];
115
		$replace = [];
116
117
		// Convert al named parameters the user
118
		// has specified as.
119
		foreach ($this->parameters as $parameter)
120
		{
121
			$search[] = '{'.$parameter->name.'}';
122
			$replace[] = $parameter->getRegex();
123
		}
124
125
		$regex = str_replace($search, $replace, $this->resource);
126
127
		// Convert remaining named parameters
128
		$regex = preg_replace('#\{([a-z\_]+)\}#', '(?P<$1>.+?)', $regex);
129
130
		return '#^'.$regex.'$#';
131
	}
132
133
	public function match($uri, $method)
134
	{
135
		if ( ! is_string($this->resource) or $method !== '*' and ! in_array($method, $this->methods))
136
		{
137
			return false;
138
		}
139
140
		$regex = $this->compileRegex();
141
142
		if ( ! preg_match($regex, $uri, $matches))
143
		{
144
			return false;
145
		}
146
147
		$translation = $this->translation;
148
		$parameters = $this->filterNamedParameters($matches);
149
150
		if (is_string($translation))
151
		{
152
			$translation = preg_replace($regex, $translation, $uri);
153
			$translation = str_replace('#/{2,}#', '/', $translation);
154
		}
155
156
		return new Match($this, $method, $uri, $translation, $parameters, $this->name);
157
	}
158
159
	public function filterNamedParameters(array $parameters)
160
	{
161
		foreach ($parameters as $key => $value)
162
		{
163
			if (is_numeric($key))
164
			{
165
				unset($parameters[$key]);
166
			}
167
		}
168
169
		return $parameters + $this->getDefaultNamedParameters();
170
	}
171
172
	public function getParameterDefault($name, $strict = true)
0 ignored issues
show
Unused Code introduced by
The parameter $strict is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
	{
174
		if ( ! isset($this->parameters[$name]))
175
		{
176
			return null;
177
		}
178
179
		$parameter = $this->parameters[$name];
180
181
		if ($parameter->optional)
182
		{
183
			return '';
184
		}
185
186
		return $parameter->default;
187
	}
188
189
	public function getDefaultNamedParameters()
190
	{
191
		return array_map(function ($parameter)
192
		{
193
			return $parameter->default;
194
		},
195
		$this->parameters);
196
	}
197
198
	public function compile(array $parameters = array())
199
	{
200
		$resource = $this->resource;
0 ignored issues
show
Unused Code introduced by
$resource is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
201
202
		$callback = function ($match) use ($parameters)
203
		{
204
			if (isset($parameters[$match[1]]))
205
			{
206
				return $parameters[$match[1]];
207
			}
208
209
			if ($default = $this->getParameterDefault($match[1]))
210
			{
211
				return $default;
212
			}
213
214
			throw new LogicException('Could not resolve parameter ['.$match[1].'] for route ['.$this->name.']');
215
		};
216
217
		return preg_replace_callback('#\{([a-zA-Z_-]+)\}#', $callback,	$this->resource);
218
	}
219
}
220