-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathInputHandler.php
More file actions
165 lines (148 loc) Β· 5.39 KB
/
InputHandler.php
File metadata and controls
165 lines (148 loc) Β· 5.39 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
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Generator;
use Intervention\Image\Colors\Cmyk\Decoders\StringColorDecoder as CmykStringColorDecoder;
use Intervention\Image\Colors\Hsl\Decoders\StringColorDecoder as HslStringColorDecoder;
use Intervention\Image\Colors\Hsv\Decoders\StringColorDecoder as HsvStringColorDecoder;
use Intervention\Image\Colors\Oklab\Decoders\StringColorDecoder as OklabStringColorDecoder;
use Intervention\Image\Colors\Oklch\Decoders\StringColorDecoder as OklchStringColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder as RgbHexColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\NamedColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder as RgbStringColorDecoder;
use Intervention\Image\Decoders\Base64ImageDecoder;
use Intervention\Image\Decoders\BinaryImageDecoder;
use Intervention\Image\Decoders\ColorObjectDecoder;
use Intervention\Image\Decoders\DataUriImageDecoder;
use Intervention\Image\Decoders\EncodedImageObjectDecoder;
use Intervention\Image\Decoders\FilePathImageDecoder;
use Intervention\Image\Decoders\StreamImageDecoder;
use Intervention\Image\Decoders\ImageObjectDecoder;
use Intervention\Image\Decoders\NativeObjectDecoder;
use Intervention\Image\Decoders\SplFileInfoImageDecoder;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\InputHandlerInterface;
class InputHandler implements InputHandlerInterface
{
/**
* All available image decoders.
*/
public const array IMAGE_DECODERS = [
ImageObjectDecoder::class,
NativeObjectDecoder::class,
StreamImageDecoder::class,
SplFileInfoImageDecoder::class,
EncodedImageObjectDecoder::class,
DataUriImageDecoder::class,
Base64ImageDecoder::class,
BinaryImageDecoder::class,
FilePathImageDecoder::class,
];
/**
* All available color decoders.
*/
public const array COLOR_DECODERS = [
NamedColorDecoder::class,
ColorObjectDecoder::class,
RgbHexColorDecoder::class,
RgbStringColorDecoder::class,
CmykStringColorDecoder::class,
HsvStringColorDecoder::class,
HslStringColorDecoder::class,
OklabStringColorDecoder::class,
OklchStringColorDecoder::class,
];
/**
* Create new input handler instance with given decoder classnames.
*
* @param array<string|DecoderInterface> $decoders
*/
public function __construct(
protected array $decoders = [],
protected ?DriverInterface $driver = null,
) {
//
}
/**
* Static factory method to create input handler for both image and color handling.
*
* @param array<string|DecoderInterface> $decoders
*/
public static function usingDecoders(array $decoders, ?DriverInterface $driver = null): self
{
return new self($decoders, $driver);
}
/**
* {@inheritdoc}
*
* @see InputHandlerInterface::handle()
*
* @throws InvalidArgumentException
* @throws NotSupportedException
* @throws DriverException
*/
public function handle(mixed $input): ImageInterface|ColorInterface
{
if ($input === null) {
throw new InvalidArgumentException('Unable to decode from null');
}
if ($input === '') {
throw new InvalidArgumentException('Unable to decode from empty string');
}
// if handler has only one single decoder run it can run directly
if (count($this->decoders) === 1) {
return $this->decoders()->current()->decode($input);
}
// multiple decoders: try to find the matching decoder for the input
foreach ($this->decoders() as $decoder) {
if ($decoder->supports($input)) {
return $decoder->decode($input);
}
}
throw new NotSupportedException('Unprocessable input');
}
/**
* Yield all decoders.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
private function decoders(): Generator
{
foreach ($this->decoders as $decoder) {
yield $this->decoder($decoder);
}
}
/**
* Resolve the given classname or object to a decoder object.
*
* @throws InvalidArgumentException
* @throws DriverException
*/
private function decoder(string|DecoderInterface $decoder): DecoderInterface
{
if (is_string($decoder)) {
if (!is_subclass_of($decoder, DecoderInterface::class)) {
throw new InvalidArgumentException('Decoder must implement ' . DecoderInterface::class);
}
$decoder = new $decoder();
}
if ($this->driver === null) {
return $decoder;
}
try {
return $this->driver->specializeDecoder($decoder);
} catch (NotSupportedException $e) {
throw new DriverException(
'Failed to resolve decoder ' . $decoder::class . ' with driver ' . $this->driver::class,
previous: $e,
);
}
}
}