Passed
Push — master ( 579a8b...9acb0f )
by Oss
01:46
created

StorageList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 102
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setInitData() 0 5 1
A getInitData() 0 4 1
A get() 0 9 2
A toArray() 0 7 2
A setList() 0 5 1
A initList() 0 4 1
1
<?php
2
/**
3
 * @category    Brownie/Util
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\Util;
9
10
/**
11
 * Storage of data in the form of a list.
12
 */
13
class StorageList
14
{
15
16
    /**
17
     * Data to initialize the list.
18
     *
19
     * @var mixed
20
     */
21
    private $initData;
22
23
    /**
24
     * List.
25
     *
26
     * @var array
27
     */
28
    private $list;
29
30
    /**
31
     * Sets incoming values.
32
     *
33
     * @param mixed     $initData   Data to initialize the list.
34
     */
35 2
    public function __construct($initData)
36
    {
37 2
        $this->setInitData($initData);
38 2
    }
39
40
    /**
41
     * Sets data to initialize the list.
42
     *
43
     * @param mixed     $initData       Data for initialization.
44
     *
45
     * @return self
46
     */
47 2
    private function setInitData($initData)
48
    {
49 2
        $this->initData = $initData;
50 2
        return $this;
51
    }
52
53
    /**
54
     * Gets data to initialize the list.
55
     *
56
     * @return mixed
57
     */
58 2
    private function getInitData()
59
    {
60 2
        return $this->initData;
61
    }
62
63
    /**
64
     * Gets value of by key name.
65
     *
66
     * @param string    $keyName            Key name.
67
     * @param mixed     $defaultValue       Default value.
68
     *
69
     * @return string|mixed
70
     */
71 1
    public function get($keyName, $defaultValue = null)
72
    {
73 1
        $list = $this->toArray();
74 1
        $keyName = strtolower($keyName);
75 1
        if (empty($list[$keyName])) {
76 1
            return $defaultValue;
77
        }
78 1
        return $list[$keyName];
79
    }
80
81
    /**
82
     * Returns the container as an array.
83
     *
84
     * @return array
85
     */
86 2
    public function toArray()
87
    {
88 2
        if (empty($this->list)) {
89 2
            $this->initList();
90
        }
91 2
        return $this->list;
92
    }
93
94
    /**
95
     * Sets the list.
96
     *
97
     * @param array     $list   List.
98
     *
99
     * @return self
100
     */
101 2
    protected function setList(array $list)
102
    {
103 2
        $this->list = $list;
104 2
        return $this;
105
    }
106
107
    /**
108
     * Initializes and sets the list.
109
     */
110 2
    protected function initList()
111
    {
112 2
        $this->setList($this->getInitData());
113 2
    }
114
}
115