1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\Rad\ResourceResolver; |
4
|
|
|
|
5
|
|
|
class RoutingNormalizer |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Normalizes string and array declarations into associative array. |
9
|
|
|
* |
10
|
|
|
* @param string|array $declaration |
11
|
|
|
* |
12
|
|
|
* @return array |
|
|
|
|
13
|
|
|
*/ |
14
|
|
|
public function normalizeDeclaration($declaration) |
15
|
|
|
{ |
16
|
|
|
if (is_string($declaration)) { |
17
|
|
|
return $this->normalizeString($declaration); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// Normalize numerically indexed array |
21
|
|
|
if (array_keys($declaration) === array_keys(array_values($declaration))) { |
22
|
|
|
return $this->normalizeArray($declaration); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
if (isset($declaration['arguments']) && !is_array($declaration['arguments'])) { |
|
|
|
|
26
|
|
|
throw new \InvalidArgumentException('The "arguments" parameter should be an array of arguments.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Adds default value to associative array |
30
|
|
|
return array_merge(['method' => null, 'required' => true, 'arguments' => []], $declaration); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function normalizeString($declaration) |
34
|
|
|
{ |
35
|
|
|
$service = $declaration; |
36
|
|
|
$method = null; |
37
|
|
|
|
38
|
|
View Code Duplication |
if (strpos($declaration, ':') !== false) { |
|
|
|
|
39
|
|
|
list($service, $method) = explode(':', $declaration); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return [ |
43
|
|
|
'service' => $service, |
44
|
|
|
'method' => $method, |
45
|
|
|
'arguments' => [], |
46
|
|
|
'required' => true, |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function normalizeArray($declaration) |
51
|
|
|
{ |
52
|
|
View Code Duplication |
if (isset($declaration[1]) && !is_array($declaration[1])) { |
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException('The second argument for a resource configuration, when expressed with a numerically indexed array, should be an array of arguments.'); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$service = $declaration[0]; |
57
|
|
|
$method = null; |
58
|
|
|
|
59
|
|
View Code Duplication |
if (false !== strpos($declaration[0], ':')) { |
|
|
|
|
60
|
|
|
list($service, $method) = explode(':', $declaration[0]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return [ |
64
|
|
|
'service' => $service, |
65
|
|
|
'method' => $method, |
66
|
|
|
'arguments' => isset($declaration[1]) ? $declaration[1] : [], |
67
|
|
|
'required' => isset($declaration[2]) ? (bool) $declaration[2] : true, |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.