Файл: vendor/intervention/image/src/InputHandler.php
Строк: 249
<?php
declare(strict_types=1);
namespace InterventionImage;
use Generator;
use InterventionImageColorsCmykDecodersStringColorDecoder as CmykStringColorDecoder;
use InterventionImageColorsHslDecodersStringColorDecoder as HslStringColorDecoder;
use InterventionImageColorsHsvDecodersStringColorDecoder as HsvStringColorDecoder;
use InterventionImageColorsOklabDecodersStringColorDecoder as OklabStringColorDecoder;
use InterventionImageColorsOklchDecodersStringColorDecoder as OklchStringColorDecoder;
use InterventionImageColorsRgbDecodersHexColorDecoder as RgbHexColorDecoder;
use InterventionImageColorsRgbDecodersNamedColorDecoder;
use InterventionImageColorsRgbDecodersStringColorDecoder as RgbStringColorDecoder;
use InterventionImageDecodersBase64ImageDecoder;
use InterventionImageDecodersBinaryImageDecoder;
use InterventionImageDecodersColorObjectDecoder;
use InterventionImageDecodersDataUriImageDecoder;
use InterventionImageDecodersEncodedImageObjectDecoder;
use InterventionImageDecodersFilePathImageDecoder;
use InterventionImageDecodersStreamImageDecoder;
use InterventionImageDecodersImageObjectDecoder;
use InterventionImageDecodersNativeObjectDecoder;
use InterventionImageDecodersSplFileInfoImageDecoder;
use InterventionImageExceptionsDriverException;
use InterventionImageExceptionsInvalidArgumentException;
use InterventionImageExceptionsNotSupportedException;
use InterventionImageInterfacesColorInterface;
use InterventionImageInterfacesDecoderInterface;
use InterventionImageInterfacesDriverInterface;
use InterventionImageInterfacesImageInterface;
use InterventionImageInterfacesInputHandlerInterface;
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,
);
}
}
}