1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2018 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Core\Service\EntityEraser; |
12
|
|
|
|
13
|
|
|
use Core\Entity\EntityInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Event for collecting dependencies. |
17
|
|
|
* |
18
|
|
|
* @author Mathias Gelhausen <[email protected]> |
19
|
|
|
* @todo write test |
20
|
|
|
*/ |
21
|
|
|
class DependencyResultEvent extends BaseEvent |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const CHECK_DEPENDENCIES = 'check-dependencies'; |
25
|
|
|
const DELETE = 'delete'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EntityInterface |
29
|
|
|
*/ |
30
|
|
|
private $entity; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DependencyResultCollection |
34
|
|
|
*/ |
35
|
|
|
private $dependencyResultCollection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* DependencyResultEvent constructor. |
39
|
|
|
* |
40
|
|
|
* @param null|string $name |
41
|
|
|
* @param null|object $target |
42
|
|
|
* @param null|\Traversable|array $params |
43
|
|
|
*/ |
44
|
|
|
public function __construct($name = null, $target = null, $params = null) |
45
|
|
|
{ |
46
|
|
|
parent::__construct($name, $target, $params); |
|
|
|
|
47
|
|
|
$this->resetDependencyResultCollection(); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @internal |
53
|
|
|
* this is needed, because Core\EventManager clones the event prototype |
54
|
|
|
* and internal object references are not cloned recursively. |
55
|
|
|
*/ |
56
|
|
|
public function __clone() |
57
|
|
|
{ |
58
|
|
|
$this->resetDependencyResultCollection(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Resets the dependencyResultCollection |
63
|
|
|
*/ |
64
|
|
|
private function resetDependencyResultCollection() |
65
|
|
|
{ |
66
|
|
|
$this->dependencyResultCollection = new DependencyResultCollection(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return EntityInterface |
72
|
|
|
*/ |
73
|
|
|
public function getEntity() |
74
|
|
|
{ |
75
|
|
|
return $this->entity; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param EntityInterface $entity |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
|
public function setEntity($entity) |
84
|
|
|
{ |
85
|
|
|
$this->entity = $entity; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the class name of the entity. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getEntityClass() |
96
|
|
|
{ |
97
|
|
|
return get_class($this->getEntity()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Checks the entitys' type. |
102
|
|
|
* |
103
|
|
|
* @param string $class Type name to check against. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isEntityInstanceOf($class) |
108
|
|
|
{ |
109
|
|
|
return $this->getEntity() instanceOf $class; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return DependencyResultCollection |
115
|
|
|
*/ |
116
|
|
|
public function getDependencyResultCollection() |
117
|
|
|
{ |
118
|
|
|
return $this->dependencyResultCollection; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Shortcut to add dependencies to the collection. |
123
|
|
|
* |
124
|
|
|
* @param string|\Traversable|array $name |
125
|
|
|
* @param null|array|\Traversable $entities |
126
|
|
|
* @param array|null $options |
127
|
|
|
* |
128
|
|
|
* @return DependencyResultCollection |
129
|
|
|
*/ |
130
|
|
|
public function addDependencies($name, $entities = null, array $options = null) |
131
|
|
|
{ |
132
|
|
|
return $this->dependencyResultCollection->add($name, $entities, $options); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns true, if this is a delete event. |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function isDelete() |
141
|
|
|
{ |
142
|
|
|
return self::DELETE == $this->getName(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function setParam($name, $value) |
146
|
|
|
{ |
147
|
|
|
if ('entity' == $name) { |
148
|
|
|
$this->setEntity($value); |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
parent::setParam($name, $value); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function setParams($params) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
if (is_array($params) || $params instanceOf \ArrayAccess) { |
158
|
|
|
if (isset($params['entity'])) { |
159
|
|
|
$this->setEntity($params['entity']); |
160
|
|
|
unset($params['entity']); |
161
|
|
|
} |
162
|
|
|
} else if (is_object($params)) { |
163
|
|
|
if (isset($params->entity)) { |
164
|
|
|
$this->setEntity($params->entity); |
165
|
|
|
unset($params->entity); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
parent::setParams($params); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.