KeyBag::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * (c) Mantas Varatiejus <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace MVar\Apache2LogParser;
11
12
/**
13
 * This is the class that holds temporary keys for preg_match(). It helps
14
 * to convert flat array to multidimensional.
15
 */
16
class KeyBag
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $storage = [];
22
23
    /**
24
     * Stores key to local storage and returns it's index
25
     *
26
     * @param string $namespace
27
     * @param string $key
28
     *
29
     * @return int Stored key index
30
     */
31 3
    public function add($namespace, $key)
32
    {
33 3
        $this->storage[$namespace][] = $key;
34
35 3
        return count($this->storage[$namespace]) - 1;
36
    }
37
38
    /**
39
     * Returns key from local storage
40
     *
41
     * @param string $namespace
42
     * @param int    $index
43
     *
44
     * @return string|null
45
     */
46 3
    public function get($namespace, $index)
47
    {
48 3
        if (!isset($this->storage[$namespace][$index])) {
49 1
            return null;
50
        }
51
52 2
        return $this->storage[$namespace][$index];
53
    }
54
55
    /**
56
     * Registers namespace
57
     *
58
     * @param string $namespace
59
     */
60 1
    public function registerNamespace($namespace)
61
    {
62 1
        $this->storage[$namespace] = [];
63 1
    }
64
65
    /**
66
     * Returns names of all stored namespaces
67
     *
68
     * @return array
69
     */
70 2
    public function getNamespaces()
71
    {
72 2
        return array_keys($this->storage);
73
    }
74
}
75