|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
9
|
|
|
* @since 0.28 |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Core\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Repository\RepositoryService; |
|
14
|
|
|
|
|
15
|
|
|
class AttachableEntityManager |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var RepositoryService |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $repositories; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $references = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param RepositoryService $repositories |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(RepositoryService $repositories) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->repositories = $repositories; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $references |
|
38
|
|
|
* @return AttachableEntityManager |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setReferences(array & $references) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->references = & $references; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Adds an $entity using an optional $key. |
|
49
|
|
|
* If $key is not provided then $entity's FQCN will be used as a key |
|
50
|
|
|
* Any existing $entity with the same $key will be replaced. |
|
51
|
|
|
* |
|
52
|
|
|
* @param IdentifiableEntityInterface $entity |
|
53
|
|
|
* @param string $key |
|
|
|
|
|
|
54
|
|
|
* @return AttachableEntityManager |
|
55
|
|
|
*/ |
|
56
|
|
|
public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$className = get_class($entity); |
|
59
|
|
|
|
|
60
|
|
|
if (! isset($key)) { |
|
61
|
|
|
$key = $className; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$reference = [ |
|
65
|
|
|
'repository' => $className |
|
66
|
|
|
]; |
|
67
|
|
|
$entityId = $entity->getId(); |
|
68
|
|
|
|
|
69
|
|
|
// check if entity is not persisted |
|
70
|
|
|
if (! $entityId) { |
|
71
|
|
|
// persist entity & retrieve its ID |
|
72
|
|
|
$this->repositories->getRepository($className)->store($entity); |
|
|
|
|
|
|
73
|
|
|
$entityId = $entity->getId(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$reference['id'] = $entityId; |
|
77
|
|
|
$this->references[$key] = $reference; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $key |
|
84
|
|
|
* @return IdentifiableEntityInterface|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getAttachedEntity($key) |
|
87
|
|
|
{ |
|
88
|
|
|
if (! isset($this->references[$key])) { |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$reference = $this->references[$key]; |
|
93
|
|
|
$entity = $this->repositories->getRepository($reference['repository']) |
|
|
|
|
|
|
94
|
|
|
->find($reference['id']); |
|
95
|
|
|
|
|
96
|
|
|
if (! $entity) { |
|
97
|
|
|
// remove reference if entity does not exists |
|
98
|
|
|
unset($this->references[$key]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $entity; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $key |
|
106
|
|
|
* @return bool Whether entity with given $key existed |
|
107
|
|
|
*/ |
|
108
|
|
|
public function removeAttachedEntity($key) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
if (isset($this->references[$key])) { |
|
111
|
|
|
unset($this->references[$key]); |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
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.