StorageArray   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 100
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A set() 0 4 1
A __call() 0 11 3
A initFields() 0 4 1
A toArray() 0 3 1
A get() 0 3 1
1
<?php
2
/**
3
 * @category    Brownie/Util
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\Util;
9
10
use Brownie\Util\Exception\UndefinedMethodException;
11
12
/**
13
 * Data storage in the form of an associative array.
14
 */
15
class StorageArray
16
{
17
18
    /**
19
     * List of supported fields.
20
     *
21
     * @var array
22
     */
23
    protected $fields = array();
24
25
    /**
26
     * Sets the incoming values.
27
     *
28
     * @param array     $fields             Set values list
29
     * @param bool      $isReinitialize     Reinitialization flag.
30
     */
31 6
    public function __construct($fields = array(), $isReinitialize = false)
32
    {
33 6
        if ($isReinitialize) {
34 4
            $this->initFields($fields);
35
        } else {
36 2
            foreach ($fields as $key => $value) {
37 1
                $method = 'set' . ucfirst($key);
38 1
                $this->$method($value);
39
            }
40
        }
41 5
    }
42
43
    /**
44
     * Call processing methods Set and Get.
45
     *
46
     * @param string    $name       Name.
47
     * @param mixed     $values     Value.
48
     *
49
     * @return mixed
50
     *
51
     * @throws UndefinedMethodException
52
     */
53 4
    public function __call($name, $values)
54
    {
55 4
        $method = substr($name, 0, 3);
56 4
        $nameField = substr($name, 3);
57 4
        if (!empty($nameField)) {
58 4
            $nameField = lcfirst($nameField);
59
        }
60 4
        if (!array_key_exists($nameField, $this->fields)) {
61 3
            throw new UndefinedMethodException('Call to undefined method ' . $name);
62
        }
63 1
        return $this->$method($nameField, $values);
64
    }
65
66
    /**
67
     * Initialization of supported fields.
68
     * Returns the current object.
69
     *
70
     * @param array     $fields     List of supported fields.
71
     *
72
     * @return self
73
     */
74 4
    private function initFields(array $fields)
75
    {
76 4
        $this->fields = $fields;
77 4
        return $this;
78
    }
79
80
    /**
81
     * Sets values in the storage.
82
     * Returns the current object.
83
     *
84
     * @param string    $name       Name.
85
     * @param mixed     $values     Value.
86
     *
87
     * @return self
88
     */
89 1
    private function set($name, $values)
90
    {
91 1
        $this->fields[$name] = $values[0];
92 1
        return $this;
93
    }
94
95
    /**
96
     * Gets the values from the storage.
97
     *
98
     * @param string    $name   Name
99
     *
100
     * @return mixed
101
     */
102 1
    private function get($name)
103
    {
104 1
        return $this->fields[$name];
105
    }
106
107
    /**
108
     * Returns the storage as an array.
109
     *
110
     * @return array
111
     */
112 3
    public function toArray()
113
    {
114 3
        return $this->fields;
115
    }
116
}
117