|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file |
|
4
|
|
|
* Contains \TheSportsDb\Entity\Factory\Factory. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace TheSportsDb\Entity\Factory; |
|
8
|
|
|
|
|
9
|
|
|
use TheSportsDb\Entity\EntityInterface; |
|
10
|
|
|
use TheSportsDb\Entity\EntityManagerConsumerTrait; |
|
11
|
|
|
use TheSportsDb\Entity\EntityManagerInterface; |
|
12
|
|
|
use TheSportsDb\Entity\Proxy\ProxyInterface; |
|
13
|
|
|
use TheSportsDb\Http\TheSportsDbClientInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Default implementation of factories. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jelle Sebreghts |
|
19
|
|
|
*/ |
|
20
|
|
|
class Factory implements FactoryInterface { |
|
21
|
|
|
|
|
22
|
|
|
use EntityManagerConsumerTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The sports db client. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \TheSportsDb\Http\TheSportsDbClientInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $sportsDbClient; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a \TheSportsDb\Facotory\Factory object. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \TheSportsDb\Http\TheSportsDbClientInterface $sportsDbClient |
|
35
|
|
|
* The sports db client to make the requests. |
|
36
|
|
|
* @param \TheSportsDb\Entity\EntityManagerInterface|null $entityManager |
|
37
|
|
|
* The entity manager. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(TheSportsDbClientInterface $sportsDbClient, EntityManagerInterface $entityManager = NULL) { |
|
40
|
|
|
$this->sportsDbClient = $sportsDbClient; |
|
41
|
|
|
if ($entityManager instanceof EntityManagerInterface) { |
|
42
|
|
|
$this->entityManager = $entityManager; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function create(\stdClass $values, $entityType) { |
|
50
|
2 |
|
$this->entityManager->sanitizeValues($values, $entityType); |
|
51
|
|
|
// Check if we should return a proxy or a full entity. |
|
52
|
2 |
|
$reflection = !$this->entityManager->isFullObject($values, $entityType) ? |
|
53
|
1 |
|
new \ReflectionClass($this->entityManager->getClass($entityType, 'proxy')) |
|
54
|
2 |
|
: new \ReflectionClass($this->entityManager->getClass($entityType)); |
|
55
|
|
|
|
|
56
|
2 |
|
$entity = $reflection->newInstance($values); |
|
57
|
2 |
|
$this->finalizeEntity($entity); |
|
58
|
|
|
|
|
59
|
2 |
|
return $entity; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Finalize the entity (or proxy). |
|
64
|
|
|
* |
|
65
|
|
|
* @param \TheSportsDb\Entity\EntityInterface $entity |
|
66
|
|
|
* Either the real or the proxy entity for this factory. |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
2 |
|
protected function finalizeEntity(EntityInterface $entity) { |
|
71
|
2 |
|
if ($entity instanceof ProxyInterface) { |
|
72
|
1 |
|
$entity->setEntityManager($this->entityManager); |
|
73
|
1 |
|
$entity->setSportsDbClient($this->sportsDbClient); |
|
74
|
|
|
} |
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Gets the entity manager of this factory. |
|
79
|
|
|
* |
|
80
|
|
|
* @return \TheSportsDb\Entity\EntityManagerInterface |
|
81
|
|
|
* The entity manager for this factory. |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getEntityManager() { |
|
84
|
1 |
|
return $this->entityManager; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|