1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 09:31:53 +02:00

Add SpecializableInterface and tests

This commit is contained in:
Oliver Vogel
2024-01-05 11:59:59 +01:00
parent 036f5e642d
commit 0fe3834385
13 changed files with 69 additions and 56 deletions

View File

@@ -4,8 +4,9 @@ namespace Intervention\Image\Analyzers;
use Intervention\Image\Interfaces\AnalyzerInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
abstract class AbstractAnalyzer implements AnalyzerInterface
abstract class AbstractAnalyzer implements AnalyzerInterface, SpecializableInterface
{
/**
* {@inheritdoc}

View File

@@ -6,8 +6,9 @@ use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
abstract class AbstractDecoder implements DecoderInterface
abstract class AbstractDecoder implements DecoderInterface, SpecializableInterface
{
public function decode(mixed $input): ImageInterface|ColorInterface
{

View File

@@ -2,16 +2,13 @@
namespace Intervention\Image\Drivers;
use Intervention\Image\Analyzers\AbstractAnalyzer;
use Intervention\Image\Decoders\AbstractDecoder;
use Intervention\Image\Encoders\AbstractEncoder;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\AnalyzerInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Modifiers\AbstractModifier;
use Intervention\Image\Interfaces\SpecializableInterface;
use ReflectionClass;
abstract class AbstractDriver implements DriverInterface
@@ -30,7 +27,7 @@ abstract class AbstractDriver implements DriverInterface
*/
public function specialize(object $input): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
{
if ($this->isExternal($input)) {
if (!($input instanceof SpecializableInterface)) {
return $input;
}
@@ -49,31 +46,4 @@ abstract class AbstractDriver implements DriverInterface
'buildSpecialized'
], $input, $this);
}
/**
* Determine if given object is external custom modifier, analyzer or encoder
*
* @param object $input
* @return bool
*/
private function isExternal(object $input): bool
{
if ($input instanceof AbstractModifier) {
return false;
}
if ($input instanceof AbstractAnalyzer) {
return false;
}
if ($input instanceof AbstractEncoder) {
return false;
}
if ($input instanceof AbstractDecoder) {
return false;
}
return true;
}
}

View File

@@ -5,11 +5,15 @@ namespace Intervention\Image\Drivers;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
class DriverSpecialized implements SpecializedInterface
abstract class DriverSpecialized implements SpecializedInterface
{
protected DriverInterface $driver;
protected object $generic;
public function __construct()
{
}
public static function buildSpecialized(object $generic, DriverInterface $driver): SpecializedInterface
{
$specialized = new static();

View File

@@ -105,9 +105,9 @@ class Driver extends AbstractDriver
*
* @see DriverInterface::handleInput()
*/
public function handleInput(mixed $input): ImageInterface|ColorInterface
public function handleInput(mixed $input, array $decoders = []): ImageInterface|ColorInterface
{
return (new InputHandler())->handle($input);
return (new InputHandler($decoders))->handle($input);
}
/**

View File

@@ -2,18 +2,19 @@
namespace Intervention\Image\Encoders;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
abstract class AbstractEncoder implements EncoderInterface
abstract class AbstractEncoder implements EncoderInterface, SpecializableInterface
{
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImage
public function encode(ImageInterface $image): EncodedImageInterface
{
return $image->encode($this);
}

View File

@@ -247,7 +247,7 @@ final class Image implements ImageInterface
*
* @see ImageInterface::encode()
*/
public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImage
public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImageInterface
{
return $this->driver->specialize($encoder)->encode($this);
}

View File

@@ -63,9 +63,9 @@ interface ImageInterface extends IteratorAggregate, Countable
* Encode image with given encoder
*
* @param EncoderInterface $encoder
* @return EncodedImage
* @return EncodedImageInterface
*/
public function encode(EncoderInterface $encoder): EncodedImage;
public function encode(EncoderInterface $encoder): EncodedImageInterface;
/**
* Save the image to the specified path in the file system. If no path is

View File

@@ -0,0 +1,7 @@
<?php
namespace Intervention\Image\Interfaces;
interface SpecializableInterface
{
}

View File

@@ -4,6 +4,8 @@ namespace Intervention\Image\Interfaces;
interface SpecializedInterface
{
public function __construct();
/**
* Return current driver instance
*

View File

@@ -4,8 +4,9 @@ namespace Intervention\Image\Modifiers;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
abstract class AbstractModifier implements ModifierInterface
abstract class AbstractModifier implements ModifierInterface, SpecializableInterface
{
/**
* {@inheritdoc}

View File

@@ -5,8 +5,6 @@ declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers;
use Intervention\Image\Drivers\DriverSpecializedEncoder;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Tests\TestCase;
use Mockery;
@@ -25,14 +23,4 @@ class DriverSpecializedEncoderTest extends TestCase
});
$this->assertEquals('result', $result);
}
public function testGetAttributes(): void
{
$encoder = Mockery::mock(DriverSpecializedEncoder::class, [
new JpegEncoder(quality: 10),
Mockery::mock(DriverInterface::class),
])->makePartial();
$this->assertEquals(10, $encoder->quality);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Drivers;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Tests\TestCase;
use Intervention\Image\Drivers\DriverSpecialized;
use Mockery;
/**
* @covers \Intervention\Image\Drivers\DriverSpecialized
*
* @internal
*/
class DriverSpecializedTest extends TestCase
{
public function testBuildSpecialized(): void
{
$generic = new JpegEncoder(quality: 10);
$driver = Mockery::mock(DriverInterface::class);
$baseclass = new class () extends DriverSpecialized
{
};
$specialized = forward_static_call(
[$baseclass, 'buildSpecialized'],
$generic,
$driver
);
$this->assertInstanceOf(JpegEncoder::class, $specialized->generic());
$this->assertInstanceOf(DriverInterface::class, $specialized->driver());
$this->assertEquals(10, $specialized->quality);
}
}