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

BaseDataType::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
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