1 | <?php |
||
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 | public function __construct(\stdClass $values) { |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function update(\stdClass $values) { |
||
57 | if ($this->entity instanceof EntityInterface) { |
||
58 | $this->entity->update($values); |
||
59 | return; |
||
60 | } |
||
61 | foreach ((array) $values as $prop => $val) { |
||
62 | if (method_exists($this, 'get' . ucfirst($prop))) { |
||
63 | $this->properties->{$prop} = $val; |
||
64 | } |
||
65 | } |
||
66 | 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 | } |
||
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 | * Lazy loads an entity. |
||
117 | * |
||
118 | * @throws \Exception |
||
119 | * When the entity is not found. |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | abstract protected function load(); |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function raw() { |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public static function getEntityType() { |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public static function getPropertyMapDefinition() { |
||
169 | } |
||
170 |