1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Contains \TheSportsDb\Entity\EntityInterface. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace TheSportsDb\Entity; |
8
|
|
|
|
9
|
|
|
use TheSportsDb\Entity\EntityManagerInterface; |
10
|
|
|
use TheSportsDb\Entity\EntityPropertyUtil; |
11
|
|
|
use TheSportsDb\PropertyMapper\PropertyMapDefinition; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Description of Entity |
15
|
|
|
* |
16
|
|
|
* @author drupalpro |
17
|
|
|
*/ |
18
|
|
|
abstract class Entity implements EntityInterface { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The property map definition. |
22
|
|
|
* |
23
|
|
|
* @var \TheSportsDb\PropertyMapper\PropertyMapDefinition |
24
|
|
|
*/ |
25
|
|
|
protected static $propertyMapDefinition; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates a new Entity object. |
29
|
|
|
* |
30
|
|
|
* @param \stdClass $values |
31
|
|
|
* The entity data. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(\stdClass $values) { |
34
|
|
|
$this->update($values); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function raw() { |
41
|
1 |
|
if (isset($this->_raw)) { |
42
|
|
|
return $this->_raw; |
43
|
|
|
} |
44
|
1 |
|
$this->_raw = new \stdClass(); |
45
|
1 |
|
$reflection = new \ReflectionClass($this); |
46
|
1 |
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
47
|
1 |
|
foreach ($methods as $method) { |
48
|
|
|
// Skip static methods. |
49
|
1 |
|
if ($method->isStatic()) { |
50
|
1 |
|
continue; |
51
|
|
|
} |
52
|
1 |
|
$methodName = $method->getName(); |
53
|
1 |
|
if (strpos($methodName, 'get') === 0) { |
54
|
1 |
|
$prop = lcfirst(substr($methodName, 3)); |
55
|
1 |
|
if (property_exists($this, $prop)) { |
56
|
1 |
|
$val = $this->{$methodName}(); |
57
|
1 |
|
$this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
1 |
|
return $this->_raw; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function update(\stdClass $values) { |
68
|
1 |
|
foreach ((array) $values as $prop => $val) { |
69
|
1 |
|
if (property_exists($this, $prop)) { |
70
|
1 |
|
$this->{$prop} = $val; |
71
|
|
|
} |
72
|
|
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public static function getEntityType() { |
79
|
1 |
|
$reflection = new \ReflectionClass(static::class); |
80
|
1 |
|
return strtolower($reflection->getShortName()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
7 |
|
public static function getPropertyMapDefinition() { |
87
|
7 |
|
if (!isset(static::$propertyMapDefinition)) { |
88
|
3 |
|
static::$propertyMapDefinition = new PropertyMapDefinition(); |
89
|
3 |
|
static::initPropertyMapDefinition(); |
90
|
|
|
} |
91
|
7 |
|
return static::$propertyMapDefinition; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Initialize the property map definition for this entity type. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
protected static function initPropertyMapDefinition() {} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Reverse map a property that is an entity. |
103
|
|
|
* |
104
|
|
|
* @param array $entity |
105
|
|
|
* The entity to reverse map. |
106
|
|
|
* @param mixed $context |
107
|
|
|
* The context for this mapping. Usually the raw entity this property is |
108
|
|
|
* from. |
109
|
|
|
* @param EntityManagerInterface $entityManager |
110
|
|
|
* The entity manager. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
* The reverse mapped value for this property, usually the entity id. |
114
|
|
|
*/ |
115
|
1 |
|
public static function reverse($entity, $context, EntityManagerInterface $entityManager) { |
116
|
1 |
|
$data = ($entity instanceof EntityInterface) ? $entity->raw() : $entity; |
117
|
1 |
|
return $data->id; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Reverse map a property that is an array of entities. |
122
|
|
|
* |
123
|
|
|
* @param array $entities |
124
|
|
|
* The entities to reverse map. |
125
|
|
|
* @param mixed $context |
126
|
|
|
* The context for this mapping. Usually the raw entity. |
127
|
|
|
* @param EntityManagerInterface $entityManager |
128
|
|
|
* The entity manager. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
* The reverse mapped value for this property, usually an array of ids. |
132
|
|
|
*/ |
133
|
1 |
|
public static function reverseArray(array $entities, $context, EntityManagerInterface $entityManager) { |
134
|
1 |
|
$reversedEntities = array(); |
135
|
1 |
|
foreach ($entities as $entity) { |
136
|
1 |
|
$reversedEntities[] = static::reverse($entity, $context, $entityManager); |
137
|
|
|
} |
138
|
1 |
|
return $reversedEntities; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Transforms a property value to an entity. |
143
|
|
|
* |
144
|
|
|
* @param mixed $value |
145
|
|
|
* The value to transform. |
146
|
|
|
* @param \stdClass $context |
147
|
|
|
* The context for this mapping. Usually the raw entity as defined by the |
148
|
|
|
* sportsdb api this property is from. |
149
|
|
|
* @param EntityManagerInterface $entityManager |
150
|
|
|
* The entity manager. |
151
|
|
|
* @param string $entityType |
152
|
|
|
* The enitity type to transform this value to. |
153
|
|
|
* @param string $idName |
154
|
|
|
* The name of the identifier property as defined by the sportsdb api. |
155
|
|
|
* @param array $contextPropertyMap |
156
|
|
|
* Extra properties to map from the context |
157
|
|
|
* |
158
|
|
|
* @return \TheSportsDb\Entity\EntityInterface |
159
|
|
|
* The mapped entity. |
160
|
|
|
*/ |
161
|
1 |
|
public static function transform($value, $context, EntityManagerInterface $entityManager, $entityType, $idName, array $contextPropertyMap = array()) { |
162
|
1 |
|
$data = static::transformHelper($value, $context, $idName, $contextPropertyMap); |
163
|
1 |
|
$entity = $entityManager->repository($entityType)->byId($data['id']); |
164
|
|
|
// Update with given values. |
165
|
1 |
|
$entity->update($data['object']); |
166
|
1 |
|
return $entity; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Helper function to transform an entity. |
171
|
|
|
* |
172
|
|
|
* @param mixed $value |
173
|
|
|
* The value to transform. |
174
|
|
|
* @param \stdClass $context |
175
|
|
|
* The context for this mapping. Usually the raw entity as defined by the |
176
|
|
|
* sportsdb api this property is from. |
177
|
|
|
* @param string $idName |
178
|
|
|
* The name of the identifier property as defined by the sportsdb api. |
179
|
|
|
* @param array $contextPropertyMap |
180
|
|
|
* Extra properties to map from the context |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* An array with following keys: |
184
|
|
|
* - object: The raw data representing the entity. |
185
|
|
|
* - id: The id of the entity. |
186
|
|
|
*/ |
187
|
1 |
|
public static function transformHelper($value, $context, $idName, array $contextPropertyMap = array()) { |
188
|
1 |
|
$data = array(); |
189
|
1 |
|
$data['id'] = is_object($value) ? $value->{$idName} : $value; |
190
|
1 |
|
$data['object'] = is_object($value) ? $value : (object) array($idName => $data['id']); |
191
|
1 |
|
foreach ($contextPropertyMap as $source => $dest) { |
192
|
1 |
|
if (isset($context->{$source})) { |
193
|
1 |
|
$data['object']->{$dest} = $context->{$source}; |
194
|
|
|
} |
195
|
|
|
} |
196
|
1 |
|
return $data; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Transform a textual representation of a date to a \DateTimeObject |
201
|
|
|
* @param string $value |
202
|
|
|
* The date. |
203
|
|
|
* @param string $format |
204
|
|
|
* The format of the date |
205
|
|
|
* @param string $time |
|
|
|
|
206
|
|
|
* |
207
|
|
|
* @return \DateTime |
208
|
|
|
*/ |
209
|
|
|
public static function transformDateTime($value, $format, $time = NULL) { |
210
|
|
|
$date = new \DateTime($value); |
211
|
|
|
$date->createFromFormat($format, $value); |
212
|
|
|
if ($time && $time !== EntityManager::EMPTYPROPERTYPLACEHOLDER) { |
|
|
|
|
213
|
|
|
$time_parts = explode(':', $time); |
|
|
|
|
214
|
|
|
$time_parts[] = 0; |
|
|
|
|
215
|
|
|
$date->setTime($time_parts[0], $time_parts[1], $time_parts[2]); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
return $date; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public static function reverseDate(\DateTime $value, $format) { |
221
|
|
|
return $value->format($format); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
public static function reverseTime($value) { |
|
|
|
|
226
|
|
|
return $value->format('H:i:s'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Transforms the date. |
231
|
|
|
* |
232
|
|
|
* @param string $value |
233
|
|
|
* The textual representation of the date. |
234
|
|
|
* |
235
|
|
|
* @return \DateTime |
236
|
|
|
*/ |
237
|
|
|
public static function transformDateDefault($value) { |
238
|
|
|
return static::transformDateTime($value, 'Y-m-d'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Reverses the date. |
243
|
|
|
* |
244
|
|
|
* @param \DateTime $value |
245
|
|
|
* The date. |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public static function reverseDateDefault(\DateTime $value) { |
250
|
|
|
return static::reverseDate($value, 'Y-m-d'); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.