|
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
|
|
|
// Adds default value to associative array |
|
26
|
|
|
return array_merge(['required' => true, 'arguments' => []], $declaration); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function normalizeString($declaration) |
|
30
|
|
|
{ |
|
31
|
|
|
$service = $declaration; |
|
32
|
|
|
$method = null; |
|
33
|
|
|
|
|
34
|
|
|
if (strpos($declaration, ':') !== false) { |
|
35
|
|
|
list($service, $method) = explode(':', $declaration); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return [ |
|
39
|
|
|
'service' => $service, |
|
40
|
|
|
'method' => $method, |
|
41
|
|
|
'arguments' => [], |
|
42
|
|
|
'required' => true, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function normalizeArray($declaration) |
|
47
|
|
|
{ |
|
48
|
|
|
$service = $declaration[0]; |
|
49
|
|
|
$method = null; |
|
50
|
|
|
|
|
51
|
|
|
if (false !== strpos($declaration[0], ':')) { |
|
52
|
|
|
throw new \RuntimeException('The first argument for a resource configuration, when expressed with a numerically indexed array, should be a string containing the service and the method used, seperated by a colon.'); |
|
|
|
|
|
|
53
|
|
|
} elseif (isset($declaration[1]) && !is_array($declaration[1])) { |
|
54
|
|
|
throw new \RuntimeException('The second argument for a resource configuration, when expressed with a numerically indexed array, should be an array of arguments.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
58
|
|
|
'service' => $service, |
|
59
|
|
|
'method' => $method, |
|
60
|
|
|
'arguments' => isset($declaration[1]) ? $declaration[1] : [], |
|
61
|
|
|
'required' => isset($declaration[2]) ? (bool) $declaration[2] : true, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.