|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MJanssen\Route; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use MJanssen\Assert\Method; |
|
7
|
|
|
|
|
8
|
|
|
class Route |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $name; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $methods = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $pattern; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $controller; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $asserts = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $values = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $methods |
|
42
|
|
|
* @param string $pattern |
|
43
|
|
|
* @param string $controller |
|
44
|
|
|
* @param array $asserts |
|
45
|
|
|
* @param array $values |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( |
|
49
|
|
|
array $methods, |
|
50
|
|
|
$pattern, |
|
51
|
|
|
$controller, |
|
52
|
|
|
array $asserts = [], |
|
53
|
|
|
array $values = [], |
|
54
|
|
|
$name = '' |
|
55
|
|
|
) { |
|
56
|
|
|
Method::assert($methods); |
|
57
|
|
|
|
|
58
|
|
|
$this->methods = $methods; |
|
59
|
|
|
$this->pattern = $pattern; |
|
60
|
|
|
$this->controller = $controller; |
|
61
|
|
|
$this->asserts = $asserts; |
|
62
|
|
|
$this->values = $values; |
|
63
|
|
|
$this->name = $name; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getName() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->name; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getMethods() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->methods; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getPattern() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->pattern; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getController() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->controller; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getAsserts() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->asserts; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getValues() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->values; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $route |
|
116
|
|
|
* @return Route |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function fromArray(array $route) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!isset($route['pattern'])) { |
|
121
|
|
|
throw new InvalidArgumentException('Required parameter pattern is not set.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (!isset($route['method'])) { |
|
125
|
|
|
throw new InvalidArgumentException('Required parameter method is not set.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (!isset($route['controller'])) { |
|
129
|
|
|
throw new InvalidArgumentException('Required parameter controller is not set.'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (isset($route['value']) && !is_array($route['value'])) { |
|
133
|
|
|
$route['value'] = []; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return new self( |
|
137
|
|
|
!is_array($route['method']) ? [$route['method']] : $route['method'], |
|
138
|
|
|
$route['pattern'], |
|
139
|
|
|
$route['controller'], |
|
140
|
|
|
isset($route['assert']) ? $route['assert'] : [], |
|
141
|
|
|
isset($route['value']) ? $route['value'] : [], |
|
142
|
|
|
isset($route['name']) ? $route['name'] : '' |
|
|
|
|
|
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.