1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage\Processor\Image; |
18
|
|
|
|
19
|
|
|
use ArrayIterator; |
20
|
|
|
use Iterator; |
21
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantExistsException; |
22
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Image\Exception\UnsupportedOperationException; |
23
|
|
|
use ReflectionClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Conversion Collection |
27
|
|
|
*/ |
28
|
|
|
class ImageVariantCollection implements ImageVariantCollectionInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array<string, \Phauthentic\Infrastructure\Storage\Processor\Image\ImageVariant> |
32
|
|
|
*/ |
33
|
|
|
protected array $variants = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
2 |
|
public static function create(): self |
39
|
|
|
{ |
40
|
2 |
|
return new self(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Workaround for php 8 because call_user_func_array will now behave this way: |
45
|
|
|
* args keys will now be interpreted as parameter names, instead of being silently ignored. |
46
|
|
|
* |
47
|
1 |
|
* @param object $object |
48
|
|
|
* @param string $method |
49
|
1 |
|
* @param array<string, mixed> $args |
50
|
|
|
* @return array<string, mixed> |
51
|
1 |
|
*/ |
52
|
1 |
|
protected static function filterArgs(object $object, string $method, array $args): array |
53
|
1 |
|
{ |
54
|
1 |
|
$filteredArgs = []; |
55
|
|
|
$variantReflection = new ReflectionClass($object); |
56
|
|
|
$methodReflection = $variantReflection->getMethod($method); |
57
|
1 |
|
$reflectionParameters = $methodReflection->getParameters(); |
58
|
|
|
|
59
|
|
|
foreach ($reflectionParameters as $parameter) { |
60
|
|
|
if (isset($args[$parameter->getName()])) { |
61
|
1 |
|
$filteredArgs[$parameter->getName()] = $args[$parameter->getName()]; |
62
|
1 |
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $filteredArgs; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
1 |
|
* @param array<string, array<string, mixed>> $variants Variant array structure |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
1 |
|
public static function fromArray(array $variants) |
73
|
|
|
{ |
74
|
|
|
$that = new self(); |
75
|
|
|
|
76
|
|
|
foreach ($variants as $name => $data) { |
77
|
|
|
$variant = ImageVariant::create($name); |
78
|
|
|
if (isset($data['optimize']) && $data['optimize'] === true) { |
79
|
2 |
|
$variant = $variant->optimize(); |
80
|
|
|
} |
81
|
2 |
|
|
82
|
|
|
if (!empty($data['path']) && is_string($data['path'])) { |
83
|
2 |
|
$variant = $variant->withPath($data['path']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($data['operations'] as $method => $args) { |
87
|
|
|
if (!method_exists($variant, $method)) { |
88
|
|
|
UnsupportedOperationException::withName($method); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
/** @var array<mixed> $parameters */ |
92
|
|
|
$parameters = self::filterArgs($variant, $method, $args); |
93
|
2 |
|
$variant = call_user_func_array( |
94
|
|
|
[$variant, $method], |
95
|
|
|
$parameters |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$that->add($variant); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
2 |
|
return $that; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
2 |
|
* @param string $name Name |
107
|
2 |
|
* @return \Phauthentic\Infrastructure\Storage\Processor\Image\ImageVariant |
108
|
|
|
*/ |
109
|
|
|
public function addNew(string $name) |
110
|
|
|
{ |
111
|
|
|
$this->add(ImageVariant::create($name)); |
112
|
|
|
|
113
|
2 |
|
return $this->get($name); |
114
|
|
|
} |
115
|
2 |
|
|
116
|
|
|
/** |
117
|
|
|
* Gets a manipulation from the collection |
118
|
|
|
* |
119
|
|
|
* @param string $name |
120
|
|
|
* @return \Phauthentic\Infrastructure\Storage\Processor\Image\ImageVariant |
121
|
1 |
|
*/ |
122
|
|
|
public function get(string $name): ImageVariant |
123
|
1 |
|
{ |
124
|
1 |
|
return $this->variants[$name]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \Phauthentic\Infrastructure\Storage\Processor\Image\ImageVariant $variant Variant |
129
|
1 |
|
* @return void |
130
|
|
|
*/ |
131
|
1 |
|
public function add(ImageVariant $variant): void |
132
|
|
|
{ |
133
|
|
|
if ($this->has($variant->name())) { |
134
|
|
|
throw VariantExistsException::withName($variant->name()); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$this->variants[$variant->name()] = $variant; |
138
|
|
|
} |
139
|
1 |
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function has(string $name): bool |
145
|
1 |
|
{ |
146
|
|
|
return isset($this->variants[$name]); |
147
|
1 |
|
} |
148
|
1 |
|
|
149
|
1 |
|
/** |
150
|
|
|
* @param string $name |
151
|
|
|
*/ |
152
|
1 |
|
public function remove(string $name): void |
153
|
|
|
{ |
154
|
|
|
unset($this->variants[$name]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
1 |
|
* @return array<string, array<string, mixed>> |
159
|
|
|
*/ |
160
|
1 |
|
public function jsonSerialize(): array |
161
|
|
|
{ |
162
|
|
|
return $this->toArray(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritDoc |
167
|
|
|
*/ |
168
|
|
|
public function getIterator(): Iterator |
169
|
|
|
{ |
170
|
|
|
return new ArrayIterator($this->variants); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return array<string, array<string, mixed>> |
175
|
|
|
*/ |
176
|
|
|
public function toArray(): array |
177
|
|
|
{ |
178
|
|
|
$array = []; |
179
|
|
|
foreach ($this->variants as $variant) { |
180
|
|
|
$array[$variant->name()] = $variant->toArray(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $array; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function count(): int |
190
|
|
|
{ |
191
|
|
|
return count($this->variants); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|