Skip to content

Commit 09f0e9f

Browse files
authored
Merge pull request #47 from ksassnowski/master
Adds support for closure inside context with enum property
2 parents 3cdb166 + 5f1eb72 commit 09f0e9f

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/Serializers/Native.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public static function wrapClosures(&$data, $storage)
242242
}
243243

244244
unset($value);
245-
} elseif (is_object($data) && ! $data instanceof static) {
245+
} elseif (is_object($data) && ! $data instanceof static && ! $data instanceof UnitEnum) {
246246
if (isset($storage[$data])) {
247247
$data = $storage[$data];
248248

tests/SerializerPhp81Test.php

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
use Tests\Fixtures\Model;
44
use Tests\Fixtures\ModelAttribute;
55

6-
enum SerializerGlobalEnum {
7-
case Admin;
8-
case Guest;
9-
case Moderator;
10-
}
11-
126
test('enums', function () {
137
$f = function (SerializerGlobalEnum $role) {
148
return $role;
@@ -57,11 +51,16 @@ enum SerializerScopedEnum {
5751
expect($f())->toBe(SerializerScopedEnum::Admin);
5852
})->with('serializers');
5953

60-
enum SerializerGlobalBackedEnum: string {
61-
case Admin = 'Administrator';
62-
case Guest = 'Guest';
63-
case Moderator = 'Moderator';
64-
}
54+
test('enums properties', function () {
55+
$object = new ClassWithEnumProperty();
56+
$f = $object->getClosure();
57+
58+
$f = s($f);
59+
60+
expect($f())
61+
->name->toBe('Admin')
62+
->value->toBeNull();
63+
})->with('serializers');
6564

6665
test('backed enums', function () {
6766
$f = function (SerializerGlobalBackedEnum $role) {
@@ -112,6 +111,17 @@ enum SerializerScopedBackedEnum: string {
112111
expect($f())->toBe(SerializerScopedBackedEnum::Admin);
113112
})->with('serializers');
114113

114+
test('backed enums properties', function () {
115+
$object = new ClassWithBackedEnumProperty();
116+
$f = $object->getClosure();
117+
118+
$f = s($f);
119+
120+
expect($f())
121+
->name->toBe('Admin')
122+
->value->toBe('Administrator');
123+
})->with('serializers');
124+
115125
test('array unpacking', function () {
116126
$f = function () {
117127
$array1 = ['a' => 1];
@@ -468,6 +478,18 @@ public function getSelf(self $instance): self
468478
}
469479
}
470480

481+
enum SerializerGlobalEnum {
482+
case Admin;
483+
case Guest;
484+
case Moderator;
485+
}
486+
487+
enum SerializerGlobalBackedEnum: string {
488+
case Admin = 'Administrator';
489+
case Guest = 'Guest';
490+
case Moderator = 'Moderator';
491+
}
492+
471493
#[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION)]
472494
class MyAttribute
473495
{
@@ -477,3 +499,26 @@ public function __construct(public $string, public $model)
477499
}
478500
}
479501

502+
class ClassWithEnumProperty
503+
{
504+
public SerializerGlobalEnum $enum = SerializerGlobalEnum::Admin;
505+
506+
public function getClosure()
507+
{
508+
return function () {
509+
return $this->enum;
510+
};
511+
}
512+
}
513+
514+
class ClassWithBackedEnumProperty
515+
{
516+
public SerializerGlobalBackedEnum $enum = SerializerGlobalBackedEnum::Admin;
517+
518+
public function getClosure()
519+
{
520+
return function () {
521+
return $this->enum;
522+
};
523+
}
524+
}

0 commit comments

Comments
 (0)