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