mirror of
https://github.com/Intervention/image.git
synced 2025-09-02 02:12:37 +02:00
Add SpecializableInterface and tests
This commit is contained in:
@@ -4,8 +4,9 @@ namespace Intervention\Image\Analyzers;
|
|||||||
|
|
||||||
use Intervention\Image\Interfaces\AnalyzerInterface;
|
use Intervention\Image\Interfaces\AnalyzerInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Interfaces\SpecializableInterface;
|
||||||
|
|
||||||
abstract class AbstractAnalyzer implements AnalyzerInterface
|
abstract class AbstractAnalyzer implements AnalyzerInterface, SpecializableInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
|
@@ -6,8 +6,9 @@ use Intervention\Image\Exceptions\DecoderException;
|
|||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\DecoderInterface;
|
use Intervention\Image\Interfaces\DecoderInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
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
|
public function decode(mixed $input): ImageInterface|ColorInterface
|
||||||
{
|
{
|
||||||
|
@@ -2,16 +2,13 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers;
|
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\Exceptions\NotSupportedException;
|
||||||
use Intervention\Image\Interfaces\AnalyzerInterface;
|
use Intervention\Image\Interfaces\AnalyzerInterface;
|
||||||
use Intervention\Image\Interfaces\DecoderInterface;
|
use Intervention\Image\Interfaces\DecoderInterface;
|
||||||
use Intervention\Image\Interfaces\DriverInterface;
|
use Intervention\Image\Interfaces\DriverInterface;
|
||||||
use Intervention\Image\Interfaces\EncoderInterface;
|
use Intervention\Image\Interfaces\EncoderInterface;
|
||||||
use Intervention\Image\Interfaces\ModifierInterface;
|
use Intervention\Image\Interfaces\ModifierInterface;
|
||||||
use Intervention\Image\Modifiers\AbstractModifier;
|
use Intervention\Image\Interfaces\SpecializableInterface;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
abstract class AbstractDriver implements DriverInterface
|
abstract class AbstractDriver implements DriverInterface
|
||||||
@@ -30,7 +27,7 @@ abstract class AbstractDriver implements DriverInterface
|
|||||||
*/
|
*/
|
||||||
public function specialize(object $input): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
|
public function specialize(object $input): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface
|
||||||
{
|
{
|
||||||
if ($this->isExternal($input)) {
|
if (!($input instanceof SpecializableInterface)) {
|
||||||
return $input;
|
return $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,31 +46,4 @@ abstract class AbstractDriver implements DriverInterface
|
|||||||
'buildSpecialized'
|
'buildSpecialized'
|
||||||
], $input, $this);
|
], $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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -5,11 +5,15 @@ namespace Intervention\Image\Drivers;
|
|||||||
use Intervention\Image\Interfaces\DriverInterface;
|
use Intervention\Image\Interfaces\DriverInterface;
|
||||||
use Intervention\Image\Interfaces\SpecializedInterface;
|
use Intervention\Image\Interfaces\SpecializedInterface;
|
||||||
|
|
||||||
class DriverSpecialized implements SpecializedInterface
|
abstract class DriverSpecialized implements SpecializedInterface
|
||||||
{
|
{
|
||||||
protected DriverInterface $driver;
|
protected DriverInterface $driver;
|
||||||
protected object $generic;
|
protected object $generic;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public static function buildSpecialized(object $generic, DriverInterface $driver): SpecializedInterface
|
public static function buildSpecialized(object $generic, DriverInterface $driver): SpecializedInterface
|
||||||
{
|
{
|
||||||
$specialized = new static();
|
$specialized = new static();
|
||||||
|
@@ -105,9 +105,9 @@ class Driver extends AbstractDriver
|
|||||||
*
|
*
|
||||||
* @see DriverInterface::handleInput()
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -2,18 +2,19 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Encoders;
|
namespace Intervention\Image\Encoders;
|
||||||
|
|
||||||
use Intervention\Image\EncodedImage;
|
use Intervention\Image\Interfaces\EncodedImageInterface;
|
||||||
use Intervention\Image\Interfaces\EncoderInterface;
|
use Intervention\Image\Interfaces\EncoderInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
|
use Intervention\Image\Interfaces\SpecializableInterface;
|
||||||
|
|
||||||
abstract class AbstractEncoder implements EncoderInterface
|
abstract class AbstractEncoder implements EncoderInterface, SpecializableInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see EncoderInterface::encode()
|
* @see EncoderInterface::encode()
|
||||||
*/
|
*/
|
||||||
public function encode(ImageInterface $image): EncodedImage
|
public function encode(ImageInterface $image): EncodedImageInterface
|
||||||
{
|
{
|
||||||
return $image->encode($this);
|
return $image->encode($this);
|
||||||
}
|
}
|
||||||
|
@@ -247,7 +247,7 @@ final class Image implements ImageInterface
|
|||||||
*
|
*
|
||||||
* @see ImageInterface::encode()
|
* @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);
|
return $this->driver->specialize($encoder)->encode($this);
|
||||||
}
|
}
|
||||||
|
@@ -63,9 +63,9 @@ interface ImageInterface extends IteratorAggregate, Countable
|
|||||||
* Encode image with given encoder
|
* Encode image with given encoder
|
||||||
*
|
*
|
||||||
* @param EncoderInterface $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
|
* Save the image to the specified path in the file system. If no path is
|
||||||
|
7
src/Interfaces/SpecializableInterface.php
Normal file
7
src/Interfaces/SpecializableInterface.php
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Intervention\Image\Interfaces;
|
||||||
|
|
||||||
|
interface SpecializableInterface
|
||||||
|
{
|
||||||
|
}
|
@@ -4,6 +4,8 @@ namespace Intervention\Image\Interfaces;
|
|||||||
|
|
||||||
interface SpecializedInterface
|
interface SpecializedInterface
|
||||||
{
|
{
|
||||||
|
public function __construct();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return current driver instance
|
* Return current driver instance
|
||||||
*
|
*
|
||||||
|
@@ -4,8 +4,9 @@ namespace Intervention\Image\Modifiers;
|
|||||||
|
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Interfaces\ModifierInterface;
|
use Intervention\Image\Interfaces\ModifierInterface;
|
||||||
|
use Intervention\Image\Interfaces\SpecializableInterface;
|
||||||
|
|
||||||
abstract class AbstractModifier implements ModifierInterface
|
abstract class AbstractModifier implements ModifierInterface, SpecializableInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
|
@@ -5,8 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace Intervention\Image\Tests\Drivers;
|
namespace Intervention\Image\Tests\Drivers;
|
||||||
|
|
||||||
use Intervention\Image\Drivers\DriverSpecializedEncoder;
|
use Intervention\Image\Drivers\DriverSpecializedEncoder;
|
||||||
use Intervention\Image\Encoders\JpegEncoder;
|
|
||||||
use Intervention\Image\Interfaces\DriverInterface;
|
|
||||||
use Intervention\Image\Tests\TestCase;
|
use Intervention\Image\Tests\TestCase;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
|
|
||||||
@@ -25,14 +23,4 @@ class DriverSpecializedEncoderTest extends TestCase
|
|||||||
});
|
});
|
||||||
$this->assertEquals('result', $result);
|
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
38
tests/Drivers/DriverSpecializedTest.php
Normal file
38
tests/Drivers/DriverSpecializedTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user