|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Manages a set of images which belongs together. |
|
19
|
|
|
* |
|
20
|
|
|
* @ODM\EmbeddedDocument |
|
21
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
22
|
|
|
* @since 0.29 |
|
23
|
|
|
*/ |
|
24
|
|
|
class ImageSet implements ImageSetInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use EntityTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The id of this image set. |
|
30
|
|
|
* |
|
31
|
|
|
* @ODM\Field(type="string") |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $id; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Images in this set. |
|
38
|
|
|
* |
|
39
|
|
|
* @ODM\ReferenceMany(discriminatorField="_entity", cascade="all", orphanRemoval=true) |
|
40
|
|
|
* @var Collection |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $images; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @internal |
|
46
|
|
|
* Creates a unique {@link $id} for this ImageSet |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->id = (string) new \MongoId(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Provide convinient methods for get and set. |
|
55
|
|
|
* |
|
56
|
|
|
* - get<ImageKey> proxies to get(<imageKey>) |
|
57
|
|
|
* - set<ImageKey>($image) proxies to set(<imageKey>, $image) |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $method |
|
60
|
|
|
* @param array $args |
|
61
|
|
|
* |
|
62
|
|
|
* @return $this|ImageInterface|null |
|
63
|
|
|
* @throws \BadMethodCallException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __call($method, $args) |
|
66
|
|
|
{ |
|
67
|
|
|
if (0 === strpos($method, 'get')) { |
|
68
|
|
|
$key = lcfirst(substr($method, 3)); |
|
69
|
|
|
return $this->get($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (0 === strpos($method, 'set')) { |
|
73
|
|
|
if (1 > count($args)) { |
|
74
|
|
|
throw new \BadMethodCallException(sprintf( |
|
75
|
|
|
'Missing argument 1 for "%s" in "%s"', $method, get_class($this) |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
$key = lcfirst(substr($method, 3)); |
|
79
|
|
|
return $this->set($key, $args[0]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
throw new \BadMethodCallException(sprintf( |
|
83
|
|
|
'Unknown method "%s" in "%s"', $method, get_class($this) |
|
84
|
|
|
)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Clear all the images in this set. |
|
89
|
|
|
* |
|
90
|
|
|
* @return self |
|
91
|
|
|
*/ |
|
92
|
|
|
public function clear() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->images) { |
|
95
|
|
|
$this->images->clear(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Collection $images |
|
103
|
|
|
* |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setImagesCollection(Collection $images) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->clear(); |
|
109
|
|
|
$this->images = $images; |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set images and permissions. |
|
116
|
|
|
* |
|
117
|
|
|
* Replaces the whole set! |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $images |
|
120
|
|
|
* @param PermissionsInterface $permissions |
|
|
|
|
|
|
121
|
|
|
* |
|
122
|
|
|
* @return self |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setImages(array $images, PermissionsInterface $permissions = null) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->clear(); |
|
127
|
|
|
|
|
128
|
|
|
foreach ($images as $prop => $image) { |
|
129
|
|
|
$this->set($prop, $image, /* check */ false); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($permissions) { |
|
133
|
|
|
$this->setPermissions($permissions); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return ArrayCollection|Collection |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getImages() |
|
143
|
|
|
{ |
|
144
|
|
|
if (!$this->images) { |
|
145
|
|
|
$this->images = new ArrayCollection(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->images; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get an image |
|
153
|
|
|
* |
|
154
|
|
|
* If no image with the $key is found, the image with the key |
|
155
|
|
|
* self::ORIGINAL is returned. Unless self::ORIGINAL is requested, |
|
156
|
|
|
* in that case NULL is returned. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $key |
|
159
|
|
|
* |
|
160
|
|
|
* @return ImageInterface|null |
|
161
|
|
|
*/ |
|
162
|
|
|
public function get($key) |
|
163
|
|
|
{ |
|
164
|
|
|
foreach ($this->getImages() as $image) { |
|
165
|
|
|
/* @var ImageInterface $image */ |
|
166
|
|
|
if ($key == $image->getKey()) { |
|
167
|
|
|
return $image; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return self::ORIGINAL == $key ? null : $this->get(self::ORIGINAL); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set an image. |
|
176
|
|
|
* |
|
177
|
|
|
* Replaces any image with the same $key, unless $check is false. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $key |
|
180
|
|
|
* @param ImageInterface $image |
|
181
|
|
|
* @param bool $check |
|
182
|
|
|
* |
|
183
|
|
|
* @return self |
|
184
|
|
|
*/ |
|
185
|
|
|
public function set($key, ImageInterface $image, $check = true) |
|
186
|
|
|
{ |
|
187
|
|
|
$images = $this->getImages(); |
|
188
|
|
|
if ($check && ($img = $this->get($key))) { |
|
189
|
|
|
$images->removeElement($img); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$image->setBelongsTo($this->id); |
|
193
|
|
|
$image->setKey($key); |
|
194
|
|
|
|
|
195
|
|
|
$images->add($image); |
|
196
|
|
|
|
|
197
|
|
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set permissions for all images in this set. |
|
202
|
|
|
* |
|
203
|
|
|
* @param PermissionsInterface $permissions |
|
204
|
|
|
* |
|
205
|
|
|
* @return self |
|
206
|
|
|
*/ |
|
207
|
|
|
public function setPermissions(PermissionsInterface $permissions) |
|
208
|
|
|
{ |
|
209
|
|
|
foreach ($this->getImages() as $file) { |
|
210
|
|
|
/* @var PermissionsInterface $filePermissions */ |
|
211
|
|
|
/* @var \Core\Entity\FileInterface $file */ |
|
212
|
|
|
$filePermissions = $file->getPermissions(); |
|
213
|
|
|
$filePermissions->clear(); |
|
214
|
|
|
$filePermissions->inherit($permissions); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
} |
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.