1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Contains \TheSportsDb\Entity\Repository\Repository. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace TheSportsDb\Entity\Repository; |
8
|
|
|
|
9
|
|
|
use TheSportsDb\Entity\EntityManagerConsumerTrait; |
10
|
|
|
use TheSportsDb\Entity\EntityManagerInterface; |
11
|
|
|
use TheSportsDb\Http\TheSportsDbClientInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract Repository implementation to inherit from. |
15
|
|
|
* |
16
|
|
|
* @author Jelle Sebreghts |
17
|
|
|
*/ |
18
|
|
|
abstract class Repository implements RepositoryInterface { |
19
|
|
|
|
20
|
|
|
use EntityManagerConsumerTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The repository of entities. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The entity type for this repository. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $entityType; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The sports db client. |
38
|
|
|
* |
39
|
|
|
* @var \TheSportsDb\Http\TheSportsDbClientInterface |
40
|
|
|
*/ |
41
|
|
|
protected $sportsDbClient; |
42
|
|
|
|
43
|
|
|
public function __construct(TheSportsDbClientInterface $sportsDbClient, EntityManagerInterface $entityManager = NULL) { |
44
|
|
|
$this->sportsDbClient = $sportsDbClient; |
45
|
|
|
if ($entityManager instanceof EntityManagerInterface) { |
46
|
|
|
$this->entityManager = $entityManager; |
47
|
|
|
} |
48
|
|
|
$this->repository = array(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
1 |
|
public function byId($id) { |
55
|
1 |
|
if (!isset($this->repository[$id])) { |
56
|
1 |
|
$factory = $this->entityManager->factory($this->getEntityTypeName()); |
57
|
1 |
|
$this->repository[$id] = $factory->create( |
58
|
1 |
|
(object) array('id' => $id), |
59
|
1 |
|
$this->getEntityTypeName() |
60
|
|
|
); |
61
|
|
|
} |
62
|
1 |
|
return $this->repository[$id]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function clear($id) { |
69
|
1 |
|
unset($this->repository[$id]); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function getEntityTypeName() { |
76
|
1 |
|
return $this->entityType; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the entity from cache or loads it & updates it with given data. |
81
|
|
|
* |
82
|
|
|
* @param \stdClass $data |
83
|
|
|
* The data of the entity. |
84
|
|
|
* |
85
|
|
|
* @return \TheSportsDb\Entity\EntityInterface |
86
|
|
|
* The matched entity. |
87
|
|
|
*/ |
88
|
24 |
|
protected function normalizeEntity(\stdClass $data) { |
89
|
24 |
|
$mapped = $this->entityManager->mapProperties($data, $this->getEntityTypeName()); |
90
|
24 |
|
if (isset($this->repository[$mapped->id])) { |
91
|
1 |
|
$entity = $this->repository[$mapped->id]; |
92
|
1 |
|
$entity->update($mapped); |
93
|
1 |
|
return $entity; |
94
|
|
|
} |
95
|
24 |
|
$factory = $this->entityManager->factory($this->getEntityTypeName()); |
96
|
24 |
|
$this->repository[$mapped->id] = $factory->create($mapped, $this->getEntityTypeName()); |
97
|
24 |
|
return $this->repository[$mapped->id]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets an array of entities based on given data. |
102
|
|
|
* |
103
|
|
|
* @see \TheSportsDb\Entity\Repository\Repository::normilizeEntity() |
104
|
|
|
* |
105
|
|
|
* @param \stdClass[]|null $data |
106
|
|
|
* The data of the entity. |
107
|
|
|
* |
108
|
|
|
* @return \TheSportsDb\Entity\EntityInterface[] |
109
|
|
|
* The matched entities. |
110
|
|
|
*/ |
111
|
24 |
|
protected function normalizeArray($data) { |
112
|
24 |
|
$normalized = array(); |
113
|
24 |
|
if ($data) { |
114
|
24 |
|
foreach ($data as $raw) { |
115
|
24 |
|
$entity = $this->normalizeEntity($raw); |
116
|
24 |
|
$normalized[$entity->getId()] = $entity; |
117
|
|
|
} |
118
|
|
|
} |
119
|
24 |
|
return $normalized; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|