diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index dabacbbf..5aad25a4 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -44,9 +44,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new InputHandler([ + return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, - ]))->handle($input); + ])->handle($input); } /** diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index 2871963f..e682f9a2 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -43,9 +43,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new InputHandler([ + return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, - ]))->handle($input); + ])->handle($input); } /** diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index d4580149..9c7f285c 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -43,9 +43,9 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new InputHandler([ + return InputHandler::withDecoders([ Decoders\StringColorDecoder::class, - ]))->handle($input); + ])->handle($input); } /** diff --git a/src/Colors/Rgb/Color.php b/src/Colors/Rgb/Color.php index 40d831ac..f97a9241 100644 --- a/src/Colors/Rgb/Color.php +++ b/src/Colors/Rgb/Color.php @@ -53,12 +53,12 @@ class Color extends AbstractColor */ public static function create(mixed $input): ColorInterface { - return (new InputHandler([ + return InputHandler::withDecoders([ Decoders\HexColorDecoder::class, Decoders\StringColorDecoder::class, Decoders\TransparentColorDecoder::class, Decoders\HtmlColornameDecoder::class, - ]))->handle($input); + ])->handle($input); } /** diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 9741bea6..8bf8cf40 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -53,7 +53,7 @@ abstract class AbstractDriver implements DriverInterface */ public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface { - return (new InputHandler($decoders, $this))->handle($input); + return InputHandler::withDecoders($decoders, $this)->handle($input); } /** diff --git a/src/InputHandler.php b/src/InputHandler.php index 1b490fa9..4c9eeed7 100644 --- a/src/InputHandler.php +++ b/src/InputHandler.php @@ -75,6 +75,18 @@ class InputHandler implements InputHandlerInterface $this->driver = $driver; } + /** + * Static factory method + * + * @param array $decoders + * @param null|DriverInterface $driver + * @return InputHandler + */ + public static function withDecoders(array $decoders, ?DriverInterface $driver = null): self + { + return new self($decoders, $driver); + } + /** * {@inheritdoc} * diff --git a/tests/Unit/InputHandlerTest.php b/tests/Unit/InputHandlerTest.php new file mode 100644 index 00000000..68a7191c --- /dev/null +++ b/tests/Unit/InputHandlerTest.php @@ -0,0 +1,62 @@ +assertInstanceOf($outputClassname, $handler->handle($input)); + } else { + $this->expectException($outputClassname); + $handler->handle($input); + } + } + + public static function testHandleProvider(): array + { + $base = [ + [null, DecoderException::class], + ['', DecoderException::class], + ['fff', ColorInterface::class], + ['rgba(0, 0, 0, 0)', ColorInterface::class], + ['cmyk(0, 0, 0, 0)', ColorInterface::class], + ['hsv(0, 0, 0)', ColorInterface::class], + ['hsl(0, 0, 0)', ColorInterface::class], + ['transparent', ColorInterface::class], + ['steelblue', ColorInterface::class], + [self::getTestResourcePath(), ImageInterface::class], + [file_get_contents(self::getTestResourcePath()), ImageInterface::class], + ]; + + $data = []; + $drivers = [GdDriver::class, ImagickDriver::class]; + foreach ($drivers as $driver) { + foreach ($base as $line) { + array_unshift($line, $driver); // prepend driver + $data[] = $line; + } + } + + return $data; + } +}