Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added __set_state for options #6

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading