1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Contains \TheSportsDb\Entity\Proxy\Proxy. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace TheSportsDb\Entity\Proxy; |
8
|
|
|
|
9
|
|
|
use TheSportsDb\Entity\EntityInterface; |
10
|
|
|
use TheSportsDb\Entity\EntityManagerConsumerTrait; |
11
|
|
|
use TheSportsDb\Entity\EntityManagerInterface; |
12
|
|
|
use TheSportsDb\Entity\EntityPropertyUtil; |
13
|
|
|
use TheSportsDb\Http\TheSportsDbClientInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default implementation of proxy objects. |
17
|
|
|
* |
18
|
|
|
* @author Jelle Sebreghts |
19
|
|
|
*/ |
20
|
|
|
abstract class Proxy implements ProxyInterface { |
21
|
|
|
|
22
|
|
|
use EntityManagerConsumerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The sports db client. |
26
|
|
|
* |
27
|
|
|
* @var \TheSportsDb\Http\TheSportsDbClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $sportsDbClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The already available properties. |
33
|
|
|
* |
34
|
|
|
* @var \stdClass |
35
|
|
|
*/ |
36
|
|
|
protected $properties; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The fully loaded entity object (lazy-loaded when needed). |
40
|
|
|
* |
41
|
|
|
* @var mixed |
42
|
|
|
*/ |
43
|
|
|
protected $entity; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
6 |
|
public function __construct(\stdClass $values) { |
49
|
6 |
|
$this->properties = new \stdClass(); |
50
|
6 |
|
$this->update($values); |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
7 |
|
public function update(\stdClass $values) { |
57
|
7 |
|
if ($this->entity instanceof EntityInterface) { |
58
|
|
|
$this->entity->update($values); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
7 |
|
foreach ((array) $values as $prop => $val) { |
62
|
7 |
|
if (method_exists($this, 'get' . ucfirst($prop))) { |
63
|
7 |
|
$this->properties->{$prop} = $val; |
64
|
|
|
} |
65
|
|
|
} |
66
|
7 |
|
if ($this->entityManager && $this->entityManager->isFullObject($this->properties, $this->getEntityType())) { |
67
|
|
|
$this->entity = $this->entityManager->factory($this->getEntityType())->create($this->properties, $this->getEntityType()); |
68
|
|
|
} |
69
|
7 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function setEntityManager(EntityManagerInterface $entityManager) { |
75
|
1 |
|
$this->entityManager = $entityManager; |
76
|
|
|
// Check if we can create a full object once the entity manager is set. |
77
|
1 |
|
$stub = new \stdClass(); |
78
|
1 |
|
$this->update($stub); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
1 |
|
public function setSportsDbClient(TheSportsDbClientInterface $sportsDbClient) { |
85
|
1 |
|
$this->sportsDbClient = $sportsDbClient; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets a property, if it exists, loads it if necessary. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* The property name. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
* The property value. |
96
|
|
|
*/ |
97
|
|
|
protected function get($name) { |
98
|
|
|
// If the full entity is loaded, use it. |
99
|
|
|
if ($this->entity instanceof EntityInterface) { |
100
|
|
|
return $this->entity->{'get' . ucfirst($name)}(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// If the property exists on the proxy, use it. |
104
|
|
|
if (isset($this->properties->{$name})) { |
105
|
|
|
return $this->properties->{$name}; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// The property does not exist on the proxy, and the entity is not loaded in |
109
|
|
|
// full yet, so load it first and repeat the operation. |
110
|
|
|
method_exists($this, 'load' . ucfirst($name)) ? $this->{'load' . ucfirst($name)}() : $this->load(); |
111
|
|
|
return $this->get($name); |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
abstract public function load(); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
1 |
|
public function raw() { |
124
|
1 |
|
if ($this->entity) { |
125
|
|
|
$this->_raw = $this->entity->raw(); |
126
|
|
|
return $this->_raw; |
127
|
|
|
} |
128
|
1 |
|
if (!isset($this->_raw)) { |
129
|
1 |
|
$this->_raw = new \stdClass(); |
130
|
|
|
} |
131
|
1 |
|
$reflection = new \ReflectionClass($this); |
132
|
1 |
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); |
133
|
1 |
|
foreach ($methods as $method) { |
134
|
1 |
|
$methodName = $method->getName(); |
135
|
1 |
|
if (strpos($methodName, 'get') === 0) { |
136
|
1 |
|
$prop = lcfirst(substr($methodName, 3)); |
137
|
1 |
|
if (isset($this->properties->{$prop}) && !property_exists($this->_raw, $prop)) { |
138
|
1 |
|
$this->_raw->{$prop} = NULL; |
139
|
1 |
|
$val = $this->{$methodName}(); |
140
|
1 |
|
$this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
1 |
|
return $this->_raw; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
1 |
|
public static function getEntityType() { |
151
|
1 |
|
$reflection = new \ReflectionClass(static::class); |
152
|
1 |
|
return strtolower(substr($reflection->getShortName(), 0, -5)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
7 |
|
public static function getPropertyMapDefinition() { |
159
|
7 |
|
$selfReflection = new \ReflectionClass(static::class); |
160
|
7 |
|
$namespace = substr($selfReflection->getNamespaceName(), 0, -5); |
161
|
7 |
|
$reflection = new \ReflectionClass($namespace . substr($selfReflection->getShortName(), 0, -5)); |
162
|
7 |
|
return $reflection->getMethod('getPropertyMapDefinition')->invoke(NULL); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|