This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataObject.php
206 lines (175 loc) · 4.88 KB
/
DataObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Synapse\Stdlib;
use BadMethodCallException;
use InvalidArgumentException;
use Synapse\Entity\AbstractEntity;
use Zend\Stdlib\ArraySerializableInterface;
abstract class DataObject implements ArraySerializableInterface
{
/**
* Entity data
*
* @var array
*/
protected $object = [];
/**
* @param array $data Initial data
*/
public function __construct(array $data = [])
{
$this->exchangeArray($data);
}
/**
* Handle magic getters and setters
*
* @param string $method Name of the method called
* @param array $args Arguments passed to the method
* @return mixed
*/
public function __call($method, array $args)
{
$type = $this->getMagicMethodType($method);
$property = $this->getMagicMethodProperty($method);
if ($type === 'get') {
// Return the property
return $this->object[$property];
}
// Not getting, so we must be setting; set the property
$this->object[$property] = $args[0];
// Fluent interface
return $this;
}
/**
* Load object data in this entity from array
*
* @param array $data Entity data to be set
* @return AbstractEntity
*/
public function exchangeArray(array $data)
{
foreach ($this->object as $key => $value) {
if (array_key_exists($key, $data)) {
$setter = $this->getSetter($key);
$this->$setter($data[$key]);
}
}
return $this;
}
/**
* Return this entity as an array
*
* @return array
*/
public function getArrayCopy()
{
return $this->object;
}
/**
* Return the method name for the setter of a given variable
*
* Example: Converts 'user_id' to 'setUserId'
*
* @param string $key Key of the variable in $this->object
* @return string
*/
protected function getSetter($key)
{
$key = str_replace('_', ' ', $key);
$key = ucwords($key);
$key = str_replace(' ', '', $key);
return 'set'.$key;
}
/**
* Determine whether the method being called is a getter or setter
*
* @param string $method Method being called
* @return string
*/
protected function getMagicMethodType($method)
{
// If the method name is less than or equal to four characters
// then it's not a getter or a setter
if (strlen($method) <= 3) {
throw new BadMethodCallException('Method not found');
}
// Whether we are setting or getting
$type = substr($method, 0, 3);
if ($type !== 'get' and $type !== 'set') {
throw new BadMethodCallException('Method not found');
}
return $type;
}
/**
* Determine the property that is being set or get
*
* @param string $method Method being called
* @return string
*/
protected function getMagicMethodProperty($method)
{
// Get the property name
$property = lcfirst(substr($method, 3));
$transform = function ($letters) {
$letter = array_shift($letters);
return '_' . strtolower($letter);
};
$property = preg_replace_callback('/([A-Z])/', $transform, $property);
// Make sure the property exists
if (! array_key_exists($property, $this->object)) {
throw new InvalidArgumentException('Property, '.$property.', not found');
}
return $property;
}
/**
* Set the given field with the given value typecasted as an integer
*
* @param string $field Field to set
* @param mixed $value Value to set
*
* @return $this
*/
protected function setAsInteger($field, $value)
{
$this->object[$field] = (int) $value;
return $this;
}
/**
* Set the given field with the given value typecasted as a float
*
* @param string $field Field to set
* @param mixed $value Value to set
*
* @return $this
*/
protected function setAsFloat($field, $value)
{
$this->object[$field] = (float) $value;
return $this;
}
/**
* Set the given field with the given value typecasted as a boolean
*
* @param string $field Field to set
* @param mixed $value Value to set
*
* @return $this
*/
protected function setAsBoolean($field, $value)
{
$this->object[$field] = (bool) $value;
return $this;
}
/**
* Set the given field with the given value typecasted as a string
*
* @param string $field Field to set
* @param mixed $value Value to set
*
* @return $this
*/
protected function setAsString($field, $value)
{
$this->object[$field] = (string) $value;
return $this;
}
}