Skip to content

Commit a1d7eda

Browse files
Merge pull request #41 from bnzo/wireable
Convert Wireable DTO with deeply nested objects to arrays
2 parents 95faf0b + 57542c6 commit a1d7eda

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/Concerns/Wireable.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ trait Wireable
1111
public static function fromLivewire($value)
1212
{
1313
if (is_array($value)) {
14+
json_decode(json_encode($value), true);
15+
1416
return new static($value);
1517
}
1618

@@ -27,6 +29,10 @@ public static function fromLivewire($value)
2729

2830
public function toLivewire(): array
2931
{
30-
return $this->toArray();
32+
$fullArray = json_decode(json_encode($this->toArray()), true);
33+
34+
$filteredArray = array_filter($fullArray, fn ($value) => ! is_null($value));
35+
36+
return $filteredArray;
3137
}
3238
}

tests/Datasets/WireableDTO.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace WendellAdriel\ValidatedDTO\Tests\Datasets;
66

7+
use Illuminate\Support\Collection;
8+
use WendellAdriel\ValidatedDTO\Casting\CollectionCast;
9+
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
710
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
811
use WendellAdriel\ValidatedDTO\Casting\StringCast;
912
use WendellAdriel\ValidatedDTO\Concerns\Wireable;
@@ -17,6 +20,10 @@ class WireableDTO extends SimpleDTO
1720

1821
public ?int $age;
1922

23+
public ?SimpleNameDTO $simple_name_dto;
24+
25+
public ?Collection $simple_names_collection;
26+
2027
protected function defaults(): array
2128
{
2229
return [];
@@ -27,6 +34,8 @@ protected function casts(): array
2734
return [
2835
'name' => new StringCast(),
2936
'age' => new IntegerCast(),
37+
'simple_name_dto' => new DTOCast(SimpleNameDTO::class),
38+
'simple_names_collection' => new CollectionCast(new DTOCast(SimpleNameDTO::class)),
3039
];
3140
}
3241
}

tests/Unit/WireableTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
declare(strict_types=1);
44

5+
use Illuminate\Support\Collection;
56
use function Pest\Faker\faker;
67
use WendellAdriel\ValidatedDTO\SimpleDTO;
8+
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleNameDTO;
79
use WendellAdriel\ValidatedDTO\Tests\Datasets\WireableDTO;
810

911
beforeEach(function () {
@@ -21,6 +23,55 @@
2123
->toBe($wireableDTO->toArray());
2224
});
2325

26+
it('validates that a Wireable DTO with a nested DTO will return the correct data for the toLivewire method', function () {
27+
$data = [
28+
'name' => $this->name,
29+
'age' => $this->age,
30+
'simple_name_dto' => [
31+
'first_name' => $this->name,
32+
'last_name' => $this->name,
33+
],
34+
];
35+
36+
$wireableDTO = new WireableDTO($data);
37+
38+
expect($wireableDTO)
39+
->toBeInstanceOf(SimpleDTO::class)
40+
->and($wireableDTO->simple_name_dto)
41+
->toBeInstanceOf(SimpleNameDTO::class)
42+
->and($wireableDTO->toLivewire())
43+
->toBe($data);
44+
});
45+
46+
it('validates that a Wireable DTO with a DTO Collection will return the correct data for the toLivewire method', function () {
47+
$simple_name = [
48+
'first_name' => $this->name,
49+
'last_name' => $this->name,
50+
];
51+
52+
$simple_name_2 = [
53+
'first_name' => $this->name,
54+
'last_name' => $this->name,
55+
];
56+
57+
$data = [
58+
'name' => $this->name,
59+
'age' => $this->age,
60+
'simple_names_collection' => [$simple_name, $simple_name_2],
61+
];
62+
63+
$wireableDTO = new WireableDTO($data);
64+
65+
expect($wireableDTO)
66+
->toBeInstanceOf(SimpleDTO::class)
67+
->and($wireableDTO->simple_names_collection)
68+
->toBeInstanceOf(Collection::class)
69+
->and($wireableDTO->simple_names_collection->first())
70+
->toBeInstanceOf(SimpleNameDTO::class)
71+
->and($wireableDTO->toLivewire())
72+
->toBe($data);
73+
});
74+
2475
it('validates that a Wireable DTO can be instantiated with the fromLivewire method with array', function () {
2576
$wireableDTO = WireableDTO::fromLivewire(['name' => $this->name, 'age' => $this->age]);
2677

0 commit comments

Comments
 (0)