-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifierFacade.php
More file actions
159 lines (131 loc) 路 5.28 KB
/
Copy pathModifierFacade.php
File metadata and controls
159 lines (131 loc) 路 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace SixtyEightPublishers\ImageStorage\Modifier\Facade;
use Intervention\Image\Image;
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
use SixtyEightPublishers\FileStorage\PathInfoInterface;
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
use SixtyEightPublishers\ImageStorage\Modifier\Applicator\ModifierApplicatorInterface;
use SixtyEightPublishers\ImageStorage\Modifier\Codec\CodecInterface;
use SixtyEightPublishers\ImageStorage\Modifier\Codec\Value\PresetValue;
use SixtyEightPublishers\ImageStorage\Modifier\Collection\ModifierCollectionInterface;
use SixtyEightPublishers\ImageStorage\Modifier\ModifierInterface;
use SixtyEightPublishers\ImageStorage\Modifier\Preset\PresetCollectionInterface;
use SixtyEightPublishers\ImageStorage\Modifier\Validator\ValidatorInterface;
use function is_array;
use function sprintf;
final class ModifierFacade implements ModifierFacadeInterface
{
/** @var array<ModifierApplicatorInterface> */
private array $applicators = [];
/** @var array<ValidatorInterface> */
private array $validators = [];
public function __construct(
private readonly ConfigInterface $config,
private readonly CodecInterface $codec,
private readonly PresetCollectionInterface $presetCollection,
private readonly ModifierCollectionInterface $modifierCollection,
) {}
public function setModifiers(array $modifiers): void
{
foreach ($modifiers as $modifier) {
if (!$modifier instanceof ModifierInterface) {
throw new InvalidArgumentException(sprintf(
'The argument passed into the method %s() must be an array of %s.',
__METHOD__,
ModifierInterface::class,
));
}
$this->modifierCollection->add($modifier);
}
}
public function setPresets(array $presets): void
{
foreach ($presets as $name => $preset) {
if (!is_array($preset)) {
throw new InvalidArgumentException(sprintf(
'The argument passed into the method %s() must be an array of arrays (a preset name => an array of modifier aliases).',
__METHOD__,
));
}
$this->presetCollection->add((string) $name, $preset);
}
}
public function setApplicators(array $applicators): void
{
foreach ($applicators as $applicator) {
if (!$applicator instanceof ModifierApplicatorInterface) {
throw new InvalidArgumentException(sprintf(
'The argument passed into the method %s() must be an array of %s.',
__METHOD__,
ModifierApplicatorInterface::class,
));
}
$this->applicators[] = $applicator;
}
}
public function setValidators(array $validators): void
{
foreach ($validators as $validator) {
if (!$validator instanceof ValidatorInterface) {
throw new InvalidArgumentException(sprintf(
'The argument passed into the method %s() must be an array of %s.',
__METHOD__,
ValidatorInterface::class,
));
}
$this->validators[] = $validator;
}
}
public function getModifierCollection(): ModifierCollectionInterface
{
return $this->modifierCollection;
}
public function getCodec(): CodecInterface
{
return $this->codec;
}
public function modifyImage(Image $image, PathInfoInterface $info, string|array $modifiers, bool $stripMeta = false): ModifyResult
{
if (!is_array($modifiers)) {
$modifiers = $this->getCodec()->decode(new PresetValue($modifiers));
}
if (empty($modifiers)) {
throw new InvalidArgumentException('Unable to modify the image, modifiers are empty.');
}
$values = $this->modifierCollection->parseValues($modifiers);
if ($stripMeta) {
$values->add('__stripMeta', true);
}
foreach ($this->validators as $validator) {
$validator->validate($values, $this->config);
}
$modified = false;
$encodeFormat = null;
$encodeQuality = null;
foreach ($this->applicators as $applicator) {
foreach ($applicator->apply($image, $info, $values, $this->config) as $key => $value) {
if (ModifierApplicatorInterface::OutImage === $key && $value instanceof Image) {
$image = $value;
$modified = true;
continue;
}
if (ModifierApplicatorInterface::OutFormat === $key) {
$encodeFormat = $value;
$modified = true;
continue;
}
if (ModifierApplicatorInterface::OutQuality === $key) {
$encodeQuality = $value;
$modified = true;
}
}
}
return new ModifyResult(
image: $image,
modified: $modified,
encodeFormat: $encodeFormat,
encodeQuality: $encodeQuality,
);
}
}