|
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
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Description of Entity |
|
14
|
|
|
* |
|
15
|
|
|
* @author drupalpro |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Entity implements EntityInterface { |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
protected static $propertyMapDefinition = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Creates a new Entity object. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \stdClass $values |
|
25
|
|
|
* The entity data. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(\stdClass $values) { |
|
28
|
|
|
$this->update($values); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function raw() { |
|
|
|
|
|
|
32
|
|
|
if (isset($this->_raw)) { |
|
33
|
|
|
return $this->_raw; |
|
34
|
|
|
} |
|
35
|
|
|
$this->_raw = new \stdClass(); |
|
36
|
|
|
$reflection = new \ReflectionClass($this); |
|
37
|
|
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
|
|
|
|
|
38
|
|
|
foreach ($methods as $method) { |
|
39
|
|
|
// Skip static methods. |
|
40
|
|
|
if ($method->isStatic()) { |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
$methodName = $method->getName(); |
|
44
|
|
|
if (strpos($methodName, 'get') === 0) { |
|
45
|
|
|
$prop = lcfirst(substr($methodName, 3)); |
|
46
|
|
|
if ($reflection->hasProperty($prop)) { |
|
47
|
|
|
$val = $this->{$methodName}(); |
|
|
|
|
|
|
48
|
|
|
$this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return $this->_raw; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function update(\stdClass $values) { |
|
56
|
|
|
foreach ((array) $values as $prop => $val) { |
|
57
|
|
|
if (property_exists($this, $prop)) { |
|
58
|
|
|
$this->{$prop} = $val; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function getEntityType() { |
|
64
|
|
|
$reflection = new \ReflectionClass(static::class); |
|
65
|
|
|
return strtolower($reflection->getShortName()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function getPropertyMapDefinition() { |
|
69
|
|
|
return static::$propertyMapDefinition; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function reverse($entity, $context, EntityManagerInterface $entityManager) { |
|
|
|
|
|
|
73
|
|
|
$data = ($entity instanceof EntityInterface) ? $entity->raw() : $entity; |
|
74
|
|
|
return $data->id; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public static function reverseArray(array $entities, $context, EntityManagerInterface $entityManager) { |
|
78
|
|
|
$reversedEntities = array(); |
|
79
|
|
|
foreach ($entities as $entity) { |
|
80
|
|
|
$reversedEntities[] = static::reverse($entity, $context, $entityManager); |
|
81
|
|
|
} |
|
82
|
|
|
return $reversedEntities; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $entityType |
|
87
|
|
|
* @param string $idName |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function transform($value, $context, EntityManagerInterface $entityManager, $entityType, $idName, array $contextPropertyMap = array()) { |
|
|
|
|
|
|
90
|
|
|
$data = static::transformHelper($value, $context, $idName, $contextPropertyMap); |
|
|
|
|
|
|
91
|
|
|
$entity = $entityManager->repository($entityType)->byId($data['id']); |
|
92
|
|
|
// Update with given values. |
|
93
|
|
|
$entity->update($data['object']); |
|
94
|
|
|
return $entity; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function transformHelper($value, $context, $idName, array $contextPropertyMap = array()) { |
|
98
|
|
|
$data = array(); |
|
|
|
|
|
|
99
|
|
|
$data['id'] = is_object($value) ? $value->{$idName} : $value; |
|
|
|
|
|
|
100
|
|
|
$data['object'] = is_object($value) ? $value : (object) array($idName => $data['id']); |
|
101
|
|
|
foreach ($contextPropertyMap as $source => $dest) { |
|
102
|
|
|
if (isset($context->{$source})) { |
|
103
|
|
|
$data['object']->{$dest} = $context->{$source}; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
return $data; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.