Completed
Pull Request — master (#34)
by Albin
02:07
created

RoutingNormalizer::normalizeString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
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.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 180 characters; contains 215 characters

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.

Loading history...
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