Skip to content

Commit 3d3fa93

Browse files
committed
Fixing issue of Serialization
1 parent 3b16480 commit 3d3fa93

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/SimpleDTO.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public function __get(string $name): mixed
9595
return $this->{$name} ?? null;
9696
}
9797

98+
public function __serialize(): array
99+
{
100+
return $this->jsonSerialize();
101+
}
102+
103+
public function __unserialize(array $data): void
104+
{
105+
$this->__construct($data);
106+
}
107+
98108
/**
99109
* Defines the default values for the properties of the DTO.
100110
*/

src/Support/ResourceCollection.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
use WendellAdriel\ValidatedDTO\Exceptions\CastException;
1313
use WendellAdriel\ValidatedDTO\Exceptions\CastTargetException;
1414

15-
/**
16-
* @internal
17-
*/
1815
final class ResourceCollection implements Responsable
1916
{
2017
public function __construct(

tests/Unit/SimpleDTOTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,19 @@ public function __invoke() {}
352352
expect($dto->inner->name)->toBe('updated inner name')
353353
->and($dto->toJson())->toBe(json_encode(['name' => 'name', 'inner' => ['name' => 'updated inner name', 'number' => 2]]));
354354
});
355+
356+
it('validates that a SimpleDTO can be instantiated from an Eloquent Model and also get serialized', function () {
357+
$model = new class() extends Model
358+
{
359+
protected $fillable = ['name'];
360+
};
361+
362+
$model->fill(['name' => $this->subject_name]);
363+
364+
$simpleDTO = SimpleDTOInstance::fromModel($model);
365+
366+
$serialized = unserialize(serialize($simpleDTO));
367+
368+
expect($serialized->validatedData)
369+
->toBe(['name' => $this->subject_name]);
370+
});

0 commit comments

Comments
 (0)