Passed
Push — master ( 94f1bd...3747ce )
by Jonathan
02:16
created

BaseDataType   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 68
ccs 34
cts 34
cp 1
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addDataType() 0 3 1
A validate() 0 2 1
A toArray() 0 10 3
A __unset() 0 3 1
A addData() 0 3 1
A __set() 0 3 1
A getType() 0 3 1
A __construct() 0 5 2
A __get() 0 3 1
A __isset() 0 3 1
1
<?php
2
3
namespace CommerceGuys\AuthNet\DataTypes;
4
5
abstract class BaseDataType implements DataTypeInterface
6
{
7
    protected $propertyMap = [];
8
    protected $properties;
9
10 29
    public function __construct(array $values = [])
11
    {
12 29
        $this->validate($values);
13 22
        foreach ($values as $name => $value) {
14 22
            $this->$name = $value;
15 22
        }
16 22
    }
17
18
    /**
19
     * Allows child classes to validate incoming values.
20
     *
21
     * @param array $values
22
     */
23 20
    protected function validate(array $values)
24
    {
25 20
    }
26
27 18
    public function toArray()
28
    {
29 18
        $sorted_properties = [];
30 18
        foreach ($this->propertyMap as $property_key) {
31 18
            if (isset($this->properties[$property_key])) {
32 18
                $sorted_properties[$property_key] = $this->properties[$property_key];
33 18
            }
34 18
        }
35 18
        $sorted_properties += $this->properties;
36 18
        return $sorted_properties;
37
    }
38
39 18
    public function getType()
40
    {
41 18
        return lcfirst((new \ReflectionClass($this))->getShortName());
42
    }
43
44 22
    public function __set($name, $value)
45
    {
46 22
        $this->properties[$name] = $value;
47 22
    }
48
49 1
    public function __get($name)
50
    {
51 1
        return $this->properties[$name];
52
    }
53
54 1
    public function __isset($name)
55
    {
56 1
        return isset($this->properties[$name]);
57
    }
58
59
60 1
    public function __unset($name)
61
    {
62 1
        unset($this->properties[$name]);
63 1
    }
64
65 2
    public function addData($name, $value)
66
    {
67 2
        $this->properties[$name] = $value;
68 2
    }
69
70 11
    public function addDataType(DataTypeInterface $data)
71
    {
72 11
        $this->properties[$data->getType()] = $data->toArray();
73 11
    }
74
}
75