From df3110bc047155df0648db2a32644d6d4bfa80b4 Mon Sep 17 00:00:00 2001 From: Alexander Karashchuk Date: Fri, 2 Aug 2024 13:47:21 +0300 Subject: [PATCH] Added __set_state for options --- .github/workflows/test.yml | 2 +- src/Options/AbstractOption.php | 25 ++++++++++++++++++++++++- src/Support/Color.php | 10 ++++++++++ src/Support/GravityType.php | 10 ++++++++++ src/Support/ImageFormat.php | 10 ++++++++++ tests/Options/BackgroundTest.php | 2 ++ tests/Options/ExtendTest.php | 2 ++ tests/Options/HeightTest.php | 2 ++ tests/Options/WidthTest.php | 2 ++ 9 files changed, 63 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2d0c218..e68daf1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - php: ['8.0', '8.1', '8.2'] + php: ['8.0', '8.1', '8.2', '8.3'] name: PHP ${{ matrix.php }} Test on ${{ matrix.os }} steps: - name: Checkout diff --git a/src/Options/AbstractOption.php b/src/Options/AbstractOption.php index 8ef95fc..5870c11 100644 --- a/src/Options/AbstractOption.php +++ b/src/Options/AbstractOption.php @@ -4,8 +4,31 @@ namespace Onliner\ImgProxy\Options; -abstract class AbstractOption +use ReflectionClass; +use Stringable; + +abstract class AbstractOption implements Stringable { + /** + * @param array $data + * + * @return static + */ + public static function __set_state(array $data): static + { + $class = new ReflectionClass(static::class); + $self = $class->newInstanceWithoutConstructor(); + + $assigner = function () use ($self, $data) { + foreach ($data as $key => $value) { + $self->{$key} = $value; + } + }; + $assigner->bindTo($self, static::class)(); + + return $self; + } + /** * @return string */ diff --git a/src/Support/Color.php b/src/Support/Color.php index 65715b5..818cba0 100644 --- a/src/Support/Color.php +++ b/src/Support/Color.php @@ -33,6 +33,16 @@ public static function fromHex(string $color): self return new self($color); } + /** + * @param array $data + * + * @return self + */ + public static function __set_state(array $data): self + { + return new self(...$data); + } + /** * @return string */ diff --git a/src/Support/GravityType.php b/src/Support/GravityType.php index 2ad422e..e1aaffc 100644 --- a/src/Support/GravityType.php +++ b/src/Support/GravityType.php @@ -50,6 +50,16 @@ public function __construct(string $type) $this->type = $type; } + /** + * @param array $data + * + * @return self + */ + public static function __set_state(array $data): self + { + return new self(...$data); + } + public function value(): string { return $this->type; diff --git a/src/Support/ImageFormat.php b/src/Support/ImageFormat.php index 0cad447..e6a9f83 100644 --- a/src/Support/ImageFormat.php +++ b/src/Support/ImageFormat.php @@ -27,6 +27,16 @@ public function __construct(string $extension) } } + /** + * @param array $data + * + * @return self + */ + public static function __set_state(array $data): self + { + return new self(...$data); + } + /** * @param string $value * diff --git a/tests/Options/BackgroundTest.php b/tests/Options/BackgroundTest.php index 24fe25a..77401c8 100644 --- a/tests/Options/BackgroundTest.php +++ b/tests/Options/BackgroundTest.php @@ -14,7 +14,9 @@ class BackgroundTest extends TestCase public function testCreate(string $color, string $expected): void { $opt = new Background($color); + $this->assertSame($expected, (string) $opt); + $this->assertEquals($opt, eval('return '.var_export($opt, true).';')); } /** diff --git a/tests/Options/ExtendTest.php b/tests/Options/ExtendTest.php index a353b17..7a2f7db 100644 --- a/tests/Options/ExtendTest.php +++ b/tests/Options/ExtendTest.php @@ -14,7 +14,9 @@ class ExtendTest extends TestCase public function testCreate(bool $extend, ?string $gravity, string $expected): void { $opt = new Extend($extend, $gravity); + $this->assertSame($expected, (string) $opt); + $this->assertEquals($opt, eval('return '.var_export($opt, true).';')); } public function testCreateDefault(): void diff --git a/tests/Options/HeightTest.php b/tests/Options/HeightTest.php index c66a886..c436ed8 100644 --- a/tests/Options/HeightTest.php +++ b/tests/Options/HeightTest.php @@ -14,7 +14,9 @@ class HeightTest extends TestCase public function testCreate(int $height, string $expected): void { $opt = new Height($height); + $this->assertSame($expected, (string) $opt); + $this->assertEquals($opt, eval('return '.var_export($opt, true).';')); } /** diff --git a/tests/Options/WidthTest.php b/tests/Options/WidthTest.php index b5d9399..4c3ddc1 100644 --- a/tests/Options/WidthTest.php +++ b/tests/Options/WidthTest.php @@ -14,7 +14,9 @@ class WidthTest extends TestCase public function testCreate(int $width, string $expected): void { $opt = new Width($width); + $this->assertSame($expected, (string) $opt); + $this->assertEquals($opt, eval('return '.var_export($opt, true).';')); } /**