|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Core\Entity; |
|
10
|
|
|
|
|
11
|
|
|
use Core\Repository\RepositoryService; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Manager for attached entities. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
17
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
18
|
|
|
* @since 0.28 |
|
19
|
|
|
* @since 0.29 Add ability to create attached entity (which are added automatically) |
|
20
|
|
|
*/ |
|
21
|
|
|
class AttachableEntityManager |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var RepositoryService |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $repositories; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $references = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param RepositoryService $repositories |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(RepositoryService $repositories) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->repositories = $repositories; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $references |
|
44
|
|
|
* @return AttachableEntityManager |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setReferences(array & $references) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->references = & $references; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Adds an $entity using an optional $key. |
|
55
|
|
|
* If $key is not provided then $entity's FQCN will be used as a key |
|
56
|
|
|
* Any existing $entity with the same $key will be replaced. |
|
57
|
|
|
* |
|
58
|
|
|
* @param IdentifiableEntityInterface $entity |
|
59
|
|
|
* @param string $key |
|
|
|
|
|
|
60
|
|
|
* @return AttachableEntityManager |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$className = get_class($entity); |
|
65
|
|
|
|
|
66
|
|
|
if (! isset($key)) { |
|
67
|
|
|
$key = $className; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$reference = [ |
|
71
|
|
|
'repository' => $className |
|
72
|
|
|
]; |
|
73
|
|
|
$entityId = $entity->getId(); |
|
74
|
|
|
|
|
75
|
|
|
// check if entity is not persisted |
|
76
|
|
|
if (! $entityId) { |
|
77
|
|
|
// persist entity & retrieve its ID |
|
78
|
|
|
$this->repositories->getRepository($className)->store($entity); |
|
|
|
|
|
|
79
|
|
|
$entityId = $entity->getId(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$reference['id'] = $entityId; |
|
83
|
|
|
$this->references[$key] = $reference; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates an entity and adds it. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $entityClass |
|
92
|
|
|
* @param array|string $values |
|
93
|
|
|
* @param null|string $key |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Core\Entity\EntityInterface |
|
96
|
|
|
* @since 0.29 |
|
97
|
|
|
*/ |
|
98
|
|
|
public function createAttachedEntity($entityClass, $values = [], $key = null) |
|
99
|
|
|
{ |
|
100
|
|
|
if (is_string($values)) { |
|
101
|
|
|
$key = $values; |
|
102
|
|
|
$values = []; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$entity = $this->repositories->getRepository($entityClass)->create($values); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$this->addAttachedEntity($entity, $key); |
|
108
|
|
|
|
|
109
|
|
|
return $entity; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @return IdentifiableEntityInterface|null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getAttachedEntity($key) |
|
117
|
|
|
{ |
|
118
|
|
|
if (! isset($this->references[$key])) { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$reference = $this->references[$key]; |
|
123
|
|
|
$entity = $this->repositories->getRepository($reference['repository']) |
|
|
|
|
|
|
124
|
|
|
->find($reference['id']); |
|
125
|
|
|
|
|
126
|
|
|
if (! $entity) { |
|
127
|
|
|
// remove reference if entity does not exists |
|
128
|
|
|
unset($this->references[$key]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $entity; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $key |
|
136
|
|
|
* @return bool Whether entity with given $key existed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function removeAttachedEntity($key) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
if (isset($this->references[$key])) { |
|
141
|
|
|
unset($this->references[$key]); |
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.