1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CommerceGuys\AuthNet\DataTypes; |
4
|
|
|
|
5
|
|
|
abstract class BaseDataType implements DataTypeInterface |
6
|
|
|
{ |
7
|
|
|
protected $propertyMap = []; |
8
|
|
|
protected $properties; |
9
|
|
|
|
10
|
20 |
|
public function __construct(array $values = []) |
11
|
|
|
{ |
12
|
20 |
|
$this->validate($values); |
13
|
18 |
|
foreach ($values as $name => $value) { |
14
|
18 |
|
$this->$name = $value; |
15
|
18 |
|
} |
16
|
18 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Allows child classes to validate incoming values. |
20
|
|
|
* |
21
|
|
|
* @param array $values |
22
|
|
|
*/ |
23
|
16 |
|
protected function validate(array $values) |
24
|
|
|
{ |
25
|
16 |
|
} |
26
|
|
|
|
27
|
14 |
|
public function toArray() |
28
|
|
|
{ |
29
|
14 |
|
$sorted_properties = []; |
30
|
14 |
|
foreach ($this->propertyMap as $property_key) { |
31
|
14 |
|
if (isset($this->properties[$property_key])) { |
32
|
14 |
|
$sorted_properties[$property_key] = $this->properties[$property_key]; |
33
|
14 |
|
} |
34
|
14 |
|
} |
35
|
14 |
|
$sorted_properties += $this->properties; |
36
|
14 |
|
return $sorted_properties; |
37
|
|
|
} |
38
|
|
|
|
39
|
14 |
|
public function getType() |
40
|
|
|
{ |
41
|
14 |
|
return lcfirst((new \ReflectionClass($this))->getShortName()); |
42
|
|
|
} |
43
|
|
|
|
44
|
18 |
|
public function __set($name, $value) |
45
|
|
|
{ |
46
|
18 |
|
$this->properties[$name] = $value; |
47
|
18 |
|
} |
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
|
1 |
|
public function addData($name, $value) |
66
|
|
|
{ |
67
|
1 |
|
$this->properties[$name] = $value; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
11 |
|
public function addDataType(DataTypeInterface $data) |
71
|
|
|
{ |
72
|
11 |
|
$this->properties[$data->getType()] = $data->toArray(); |
73
|
11 |
|
} |
74
|
|
|
} |
75
|
|
|
|