1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 13:32:56 +02:00

Add InputHandlerInterface

This commit is contained in:
Oliver Vogel
2023-11-04 08:57:28 +01:00
parent 79d92225e8
commit aae6d359aa
3 changed files with 39 additions and 17 deletions

View File

@@ -6,8 +6,9 @@ use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\InputHandlerInterface;
abstract class AbstractInputHandler
abstract class AbstractInputHandler implements InputHandlerInterface
{
/**
* Array of decoders which will be stacked into to the input handler chain
@@ -15,15 +16,25 @@ abstract class AbstractInputHandler
protected array $decoders = [];
/**
* Create new instance
* {@inheritdoc}
*
* @param array $decoders
* @see InputHandlerInterface::__construct()
*/
public function __construct(array $decoders = [])
{
$this->decoders = count($decoders) ? $decoders : $this->decoders;
}
/**
* {@inheritdoc}
*
* @see InputHandlerInterface::handle()
*/
public function handle($input): ImageInterface|ColorInterface
{
return $this->chain()->handle($input);
}
/**
* Stack the decoder array into a nested decoder object
*
@@ -46,15 +57,4 @@ abstract class AbstractInputHandler
return $chain;
}
/**
* Try to decode the given input with each decoder of the the handler chain
*
* @param mixed $input
* @return ImageInterface|ColorInterface
*/
public function handle($input): ImageInterface|ColorInterface
{
return $this->chain()->handle($input);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Intervention\Image\Interfaces;
interface InputHandlerInterface
{
/**
* Create new instance with an array of decoders
*
* @param array $decoders
*/
public function __construct(array $decoders = []);
/**
* Try to decode the given input with each decoder of the the handler chain
*
* @param mixed $input
* @return ImageInterface|ColorInterface
*/
public function handle($input): ImageInterface|ColorInterface;
}

View File

@@ -29,15 +29,16 @@ final class AbstractInputHandlerTest extends TestCase
private function getModifier(AbstractDecoder $chain): AbstractInputHandler
{
return new class ($chain) extends AbstractInputHandler {
public function __construct(private AbstractDecoder $chain)
return new class ([$chain]) extends AbstractInputHandler
{
public function __construct(protected array $decoders = [])
{
//
}
protected function chain(): AbstractDecoder
{
return $this->chain;
return $this->decoders[0];
}
};
}