NamespacedItemResolver::setParsedKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Magister\Services\Support;
4
5
/**
6
 * Class NamespacedItemResolver.
7
 */
8
class NamespacedItemResolver
9
{
10
    /**
11
     * A cache of the parsed items.
12
     *
13
     * @var array
14
     */
15
    protected $parsed = [];
16
17
    /**
18
     * Parse a key into namespace, group, and item.
19
     *
20
     * @param string $key
21
     *
22
     * @return array
23
     */
24
    public function parseKey($key)
25
    {
26
        if (isset($this->parsed[$key])) {
27
            return $this->parsed[$key];
28
        }
29
30
        if (strpos($key, '::') === false) {
31
            $segments = explode('.', $key);
32
33
            $parsed = $this->parseBasicSegments($segments);
34
        } else {
35
            $parsed = $this->parseNamespacedSegments($key);
36
        }
37
38
        return $this->parsed[$key] = $parsed;
39
    }
40
41
    /**
42
     * Parse an array of basic segments.
43
     *
44
     * @param array $segments
45
     *
46
     * @return array
47
     */
48
    protected function parseBasicSegments(array $segments)
49
    {
50
        $group = $segments[0];
51
52
        if (count($segments) == 1) {
53
            return [null, $group, null];
54
        } else {
55
            $item = implode('.', array_slice($segments, 1));
56
57
            return [null, $group, $item];
58
        }
59
    }
60
61
    /**
62
     * Parse an array of namespaced segments.
63
     *
64
     * @param string $key
65
     *
66
     * @return array
67
     */
68
    protected function parseNamespacedSegments($key)
69
    {
70
        list($namespace, $item) = explode('::', $key);
71
72
        $itemSegments = explode('.', $item);
73
74
        $groupAndItem = array_slice($this->parseBasicSegments($itemSegments), 1);
75
76
        return array_merge([$namespace], $groupAndItem);
77
    }
78
79
    /**
80
     * Set the parsed value of a key.
81
     *
82
     * @param string $key
83
     * @param array  $parsed
84
     *
85
     * @return void
86
     */
87
    public function setParsedKey($key, $parsed)
88
    {
89
        $this->parsed[$key] = $parsed;
90
    }
91
}
92