Skip to content

Commit

Permalink
Added __set_state for options (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarashchuk authored Aug 19, 2024
1 parent ac14b0a commit 31b29e0
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion src/Options/AbstractOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@

namespace Onliner\ImgProxy\Options;

abstract class AbstractOption
use ReflectionClass;
use Stringable;

abstract class AbstractOption implements Stringable
{
/**
* @param array<string, mixed> $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
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Support/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public static function fromHex(string $color): self
return new self($color);
}

/**
* @param array<string, mixed> $data
*
* @return self
*/
public static function __set_state(array $data): self
{
return new self(...$data);
}

/**
* @return string
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Support/GravityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public function __construct(string $type)
$this->type = $type;
}

/**
* @param array<string, mixed> $data
*
* @return self
*/
public static function __set_state(array $data): self
{
return new self(...$data);
}

public function value(): string
{
return $this->type;
Expand Down
10 changes: 10 additions & 0 deletions src/Support/ImageFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public function __construct(string $extension)
}
}

/**
* @param array<string, mixed> $data
*
* @return self
*/
public static function __set_state(array $data): self
{
return new self(...$data);
}

/**
* @param string $value
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Options/BackgroundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).';'));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Options/ExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/Options/HeightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).';'));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Options/WidthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).';'));
}

/**
Expand Down

0 comments on commit 31b29e0

Please sign in to comment.