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 | 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) { |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 1 | public function setSportsDbClient(TheSportsDbClientInterface $sportsDbClient) { |
|
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) { |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | abstract public function load(); |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 1 | public function raw() { |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | public static function getEntityType() { |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 7 | public static function getPropertyMapDefinition() { |
|
164 | } |
||
165 |