From 74f6921b1ca1f882fee6f5367240c1bf8e481fdb Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 22 Oct 2022 20:56:07 +0300 Subject: [PATCH 1/2] Added `toJson` method --- src/Concerns/To.php | 13 +++++++++++++ src/DataTransferObject.php | 4 +++- tests/Unit/ToJsonTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/Concerns/To.php create mode 100644 tests/Unit/ToJsonTest.php diff --git a/src/Concerns/To.php b/src/Concerns/To.php new file mode 100644 index 0000000..9c7ee49 --- /dev/null +++ b/src/Concerns/To.php @@ -0,0 +1,13 @@ +toArray(), $flags); + } +} diff --git a/src/DataTransferObject.php b/src/DataTransferObject.php index b0b0a12..e8f33e1 100644 --- a/src/DataTransferObject.php +++ b/src/DataTransferObject.php @@ -8,6 +8,7 @@ use DragonCode\SimpleDataTransferObject\Concerns\Castable; use DragonCode\SimpleDataTransferObject\Concerns\From; use DragonCode\SimpleDataTransferObject\Concerns\Reflection; +use DragonCode\SimpleDataTransferObject\Concerns\To; use DragonCode\Support\Concerns\Makeable; use DragonCode\Support\Facades\Helpers\Arr; use DragonCode\Support\Facades\Helpers\Str; @@ -23,6 +24,7 @@ abstract class DataTransferObject implements Contract use From; use Makeable; use Reflection; + use To; protected $map = []; @@ -35,7 +37,7 @@ abstract class DataTransferObject implements Contract */ public function __construct(array $items = []) { - $this->merge($items); + $this->merge($items); } public function get(string $key) diff --git a/tests/Unit/ToJsonTest.php b/tests/Unit/ToJsonTest.php new file mode 100644 index 0000000..23e2a28 --- /dev/null +++ b/tests/Unit/ToJsonTest.php @@ -0,0 +1,24 @@ + $this->foo, + 'bar' => $this->bar, + 'baz' => $this->baz, + ]); + + $this->assertJson($object->toJson()); + + $this->assertSame('{"foo":"Foo"}', $object->toJson()); + } +} From 49ba86b4cb5dce9c648fdeaeb01a8c0798acb732 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 22 Oct 2022 20:57:33 +0300 Subject: [PATCH 2/2] Fixed code-style --- src/DataTransferObject.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/DataTransferObject.php b/src/DataTransferObject.php index e8f33e1..ce7d005 100644 --- a/src/DataTransferObject.php +++ b/src/DataTransferObject.php @@ -61,7 +61,9 @@ public function set(string $key, $value): DataTransferObject } /** - * @throws \ReflectionException + * @param array $items + * + * @throws ReflectionException */ public function merge(array $items): DataTransferObject {