ArrayList::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
/**
3
 * @category    Brownie/CartsGuru
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\CartsGuru\Model\Base;
9
10
use Brownie\CartsGuru\Exception\UndefinedMethodException;
11
12
/**
13
 * Storage array.
14
 */
15
abstract class ArrayList
16
{
17
18
    /**
19
     * List of supported fields.
20
     *
21
     * @var array
22
     */
23
    protected $fields = array();
24
25 17
    public function __construct($values = array())
26
    {
27 17
        foreach ($values as $key => $value) {
28 3
            $method = 'set' . ucfirst($key);
29 3
            $this->$method($value);
30
        }
31 17
    }
32
33 15
    public function __call($name, $values)
34
    {
35 15
        $method = substr($name, 0, 3);
36 15
        $nameField = lcfirst(substr($name, 3));
37 15
        if (!array_key_exists($nameField, $this->fields)) {
38 1
            throw new UndefinedMethodException('Call to undefined method ' . $name);
39
        }
40 15
        return $this->$method($nameField, $values);
41
    }
42
43 15
    private function set($name, $values)
44
    {
45 15
        $this->fields[$name] = $values[0];
46 15
        return $this;
47
    }
48
49 8
    private function get($name)
50
    {
51 8
        return $this->fields[$name];
52
    }
53
54
    /**
55
     * Returns the field list as an array.
56
     *
57
     * @return array
58
     */
59 8
    public function toArray()
60
    {
61 8
        return $this->fields;
62
    }
63
}
64