Completed
Push — 4.0 ( 86f02a...910ce5 )
by Olivier
04:49
created

RouteDefinition::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Routing;
13
14
use ICanBoogie\HTTP\Request;
15
16
use function ICanBoogie\format;
17
18
/**
19
 * The class defines options that can be used to define a route as well as means to normalize and
20
 * validate this definition.
21
 */
22
class RouteDefinition
23
{
24
	/**
25
	 * Pattern of the route.
26
	 */
27
	const PATTERN = 'pattern';
28
29
	/**
30
	 * A controller class name (with an optional action) or a callable.
31
	 */
32
	const CONTROLLER = 'controller';
33
34
	/**
35
	 * The controller action.
36
	 */
37
	const ACTION = 'action';
38
39
	/**
40
	 * An identifier.
41
	 */
42
	const ID = 'id';
43
44
	/**
45
	 * A redirection target.
46
	 */
47
	const LOCATION = 'location';
48
49
	/**
50
	 * Request method(s) accepted by the route.
51
	 */
52
	const VIA = 'via';
53
54
	/**
55
	 * Route constructor, a class name for now.
56
	 */
57
	const CONSTRUCTOR = 'class';
58
59
	/**
60
	 * Normalizes a route definition.
61
	 *
62
	 * @param array $definition
63
	 */
64
	static public function normalize(array &$definition)
65
	{
66
		if (empty($definition[self::VIA]))
67
		{
68
			$definition[self::VIA] = Request::METHOD_ANY;
69
		}
70
	}
71
72
	/**
73
	 * Ensures that a route definition has an identifier and generates one if required.
74
	 *
75
	 * @param array $definition
76
	 *
77
	 * @return string The route identifier.
78
	 */
79
	static public function ensure_has_id(array &$definition)
80
	{
81
		if (empty($definition[self::ID]))
82
		{
83
			$definition[self::ID] = self::generate_anonymous_id();
84
		}
85
86
		return $definition[self::ID];
87
	}
88
89
	static private $anonymous_id_count;
90
91
	/**
92
	 * Generates an anonymous route identifier.
93
	 *
94
	 * @return string
95
	 */
96
	static private function generate_anonymous_id()
97
	{
98
		return 'anonymous_route_' . ++self::$anonymous_id_count;
99
	}
100
101
	/**
102
	 * Asserts that a route definition is valid.
103
	 *
104
	 * @param array $definition
105
	 *
106
	 * @throws PatternNotDefined when the pattern is not defined
107
	 * @throws ControllerNotDefined when both controller and location are not defined.
108
	 */
109
	static public function assert_is_valid(array $definition)
110
	{
111 View Code Duplication
		if (empty($definition[self::PATTERN]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
		{
113
			throw new PatternNotDefined(format("Pattern is not defined: !route", [
114
115
				'route' => $definition
116
117
			]));
118
		}
119
120 View Code Duplication
		if (empty($definition[self::CONTROLLER]) && empty($definition[self::LOCATION]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
		{
122
			throw new ControllerNotDefined(format("Controller is not defined: !route", [
123
124
				'route' => $definition
125
126
			]));
127
		}
128
	}
129
130
	/**
131
	 * No instance should be created from this class.
132
	 *
133
	 * @codeCoverageIgnore
134
	 */
135
	private function __construct()
136
	{
137
138
	}
139
}
140