Parameter::setDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
class Parameter
14
{
15
	public $default;
16
	public $name;
17
	public $regex;
18
	public $route;
19
	public $optional;
20
21
	public function __construct(Route $route = null, $name, $regex, $optional = false)
22
	{
23
		$this->route = $route;
24
		$this->name = $name;
25
		$this->regex = $regex;
26
		$this->optional = $optional;
27
	}
28
29
	public function isOptional($optional = true)
30
	{
31
		$this->optional = $optional;
32
33
		return $this;
34
	}
35
36
	public function setDefault($default)
37
	{
38
		$this->default = $default;
39
40
		return $this;
41
	}
42
43
	public function matches($regex)
44
	{
45
		$this->regex = $regex;
46
47
		return $this;
48
	}
49
50
	public function getRegex()
51
	{
52
		$regex = '(?P<'.$this->name.'>'.$this->regex.')';
53
54
		if ($this->optional)
55
		{
56
			$regex = '(?:'.$regex.')?';
57
		}
58
59
		return $regex;
60
	}
61
62
	public function parameter()
63
	{
64
		if ( ! $this->route)
65
		{
66
			throw new \LogicException('Parameter chaining required a $route object to be set.');
67
		}
68
69
		return call_user_func_array(array($this->route, 'parameter'), func_get_args());
70
	}
71
}
72