Skip to content

Commit b61a14d

Browse files
author
liutao
committed
support enum
1 parent ccce34a commit b61a14d

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/Data.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ public function __debugInfo(): ?array
3939
return $this->objectToArray($this, false);
4040
}
4141

42-
public function __serialize(): array
43-
{
44-
return $this->toArray();
45-
}
46-
47-
/**
48-
* @throws Exception
49-
*/
50-
public function __unserialize(array $data)
51-
{
52-
$this->fill($data);
53-
}
5442

5543
public static function from(array|Arrayable $data): static
5644
{
@@ -82,14 +70,17 @@ public function fill(array $data): static
8270
$value = $data[$camelCasePropertyName] ?? ($data[$snakePropertyName] ?? null);
8371
if ($type->isBuiltin() && !is_null($value)) {
8472
$property->setValue($this, $value);
85-
}
86-
if (class_exists($type->getName())) {
73+
} elseif (PHP_VERSION_ID > 80100 && enum_exists($type->getName())) {
74+
$property->setValue($this, $value);
75+
} elseif (class_exists($type->getName())) {
8776
if (is_array($value)) {
8877
$instance = new ($type->getName());
8978
if ($instance instanceof Data) {
9079
$instance->fill($value);
9180
}
9281
$property->setValue($this, $instance);
82+
} else {
83+
$property->setValue($this, $value);
9384
}
9485
}
9586
}
@@ -121,7 +112,11 @@ protected function propertyToArray(object $object, bool $toSnake, ReflectionProp
121112
continue;
122113
}
123114
$name = $toSnake ? Str::snake($property->getName()) : $property->getName();
124-
$result[$name] = $this->forValue($property->getValue($object), $toSnake);
115+
if ($property->isInitialized($object)) {
116+
$result[$name] = $this->forValue($property->getValue($object), $toSnake);
117+
} else {
118+
$result[$name] = null;
119+
}
125120
}
126121
return $result;
127122
}
@@ -156,7 +151,10 @@ protected function getReflectionClass(object|string $object): ReflectionClass
156151

157152
protected function getStaticReflection(): ReflectionClass
158153
{
159-
return $this->_staticReflection ?? $this->getReflectionClass($this);
154+
if ($this->_staticReflection) {
155+
return $this->_staticReflection;
156+
}
157+
return $this->_staticReflection = $this->getReflectionClass($this);
160158
}
161159

162160
protected function isInsideProperty(ReflectionProperty $property): bool

src/Traits/ArrayAccessTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Ltaooo\Data\Traits;
44

5+
use ReturnTypeWillChange;
6+
57
trait ArrayAccessTrait
68
{
79
public function offsetExists($offset): bool
810
{
911
return isset($this->{$offset});
1012
}
1113

14+
#[ReturnTypeWillChange]
1215
public function offsetGet($offset)
1316
{
1417
return $this->{$offset};

tests/Feature/DataTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
});
1717

1818
test('serialize', function () {
19-
2019
$value = ['name' => '123', 'phones' => [1, 2], 'b' => ['name' => 'hello'], 'address_detail' => 'haa'];
2120
$a = A::from($value);
2221
/** @var A $b */
2322
$b = unserialize(serialize($a));
2423
expect($b)->toBeInstanceOf(A::class)
2524
->and($b->name)->toBe($a->name)
2625
->and($b->addressDetail)->toBe($a->addressDetail);
26+
27+
$c = C::from(['name' => '123', 'a' => enumA::A]);
28+
var_dump(serialize($c));
29+
$c1 = unserialize(serialize($c));
30+
var_dump($c1);
31+
expect($c1)->toBeInstanceOf(C::class);
2732
});
2833

2934
test('str', function () {
@@ -48,4 +53,17 @@ class A extends Data
4853
class B extends Data
4954
{
5055
public string $name;
56+
57+
}
58+
59+
class C extends Data
60+
{
61+
public string $name;
62+
63+
public EnumA $a;
64+
}
65+
66+
enum enumA: string
67+
{
68+
case A = 'a';
5169
}

0 commit comments

Comments
 (0)