Skip to content

Commit 31b29e0

Browse files
authored
Added __set_state for options (#6)
1 parent ac14b0a commit 31b29e0

9 files changed

Lines changed: 63 additions & 2 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
matrix:
1010
os: [ubuntu-latest]
11-
php: ['8.0', '8.1', '8.2']
11+
php: ['8.0', '8.1', '8.2', '8.3']
1212
name: PHP ${{ matrix.php }} Test on ${{ matrix.os }}
1313
steps:
1414
- name: Checkout

src/Options/AbstractOption.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@
44

55
namespace Onliner\ImgProxy\Options;
66

7-
abstract class AbstractOption
7+
use ReflectionClass;
8+
use Stringable;
9+
10+
abstract class AbstractOption implements Stringable
811
{
12+
/**
13+
* @param array<string, mixed> $data
14+
*
15+
* @return static
16+
*/
17+
public static function __set_state(array $data): static
18+
{
19+
$class = new ReflectionClass(static::class);
20+
$self = $class->newInstanceWithoutConstructor();
21+
22+
$assigner = function () use ($self, $data) {
23+
foreach ($data as $key => $value) {
24+
$self->{$key} = $value;
25+
}
26+
};
27+
$assigner->bindTo($self, static::class)();
28+
29+
return $self;
30+
}
31+
932
/**
1033
* @return string
1134
*/

src/Support/Color.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public static function fromHex(string $color): self
3333
return new self($color);
3434
}
3535

36+
/**
37+
* @param array<string, mixed> $data
38+
*
39+
* @return self
40+
*/
41+
public static function __set_state(array $data): self
42+
{
43+
return new self(...$data);
44+
}
45+
3646
/**
3747
* @return string
3848
*/

src/Support/GravityType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public function __construct(string $type)
5050
$this->type = $type;
5151
}
5252

53+
/**
54+
* @param array<string, mixed> $data
55+
*
56+
* @return self
57+
*/
58+
public static function __set_state(array $data): self
59+
{
60+
return new self(...$data);
61+
}
62+
5363
public function value(): string
5464
{
5565
return $this->type;

src/Support/ImageFormat.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public function __construct(string $extension)
2727
}
2828
}
2929

30+
/**
31+
* @param array<string, mixed> $data
32+
*
33+
* @return self
34+
*/
35+
public static function __set_state(array $data): self
36+
{
37+
return new self(...$data);
38+
}
39+
3040
/**
3141
* @param string $value
3242
*

tests/Options/BackgroundTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class BackgroundTest extends TestCase
1414
public function testCreate(string $color, string $expected): void
1515
{
1616
$opt = new Background($color);
17+
1718
$this->assertSame($expected, (string) $opt);
19+
$this->assertEquals($opt, eval('return '.var_export($opt, true).';'));
1820
}
1921

2022
/**

tests/Options/ExtendTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class ExtendTest extends TestCase
1414
public function testCreate(bool $extend, ?string $gravity, string $expected): void
1515
{
1616
$opt = new Extend($extend, $gravity);
17+
1718
$this->assertSame($expected, (string) $opt);
19+
$this->assertEquals($opt, eval('return '.var_export($opt, true).';'));
1820
}
1921

2022
public function testCreateDefault(): void

tests/Options/HeightTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class HeightTest extends TestCase
1414
public function testCreate(int $height, string $expected): void
1515
{
1616
$opt = new Height($height);
17+
1718
$this->assertSame($expected, (string) $opt);
19+
$this->assertEquals($opt, eval('return '.var_export($opt, true).';'));
1820
}
1921

2022
/**

tests/Options/WidthTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class WidthTest extends TestCase
1414
public function testCreate(int $width, string $expected): void
1515
{
1616
$opt = new Width($width);
17+
1718
$this->assertSame($expected, (string) $opt);
19+
$this->assertEquals($opt, eval('return '.var_export($opt, true).';'));
1820
}
1921

2022
/**

0 commit comments

Comments
 (0)