From fd3cc592c63f6e36d2fc093a4dbd5286beba1929 Mon Sep 17 00:00:00 2001 From: Patrick Thomas Date: Mon, 10 Feb 2025 19:10:20 +0100 Subject: [PATCH 1/5] Fixing issue 100 - Exception: Serialization of 'Closure' is not allowed #100 --- src/SimpleDTO.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/SimpleDTO.php b/src/SimpleDTO.php index 4f39567..aac5285 100644 --- a/src/SimpleDTO.php +++ b/src/SimpleDTO.php @@ -559,4 +559,14 @@ private function isforbiddenProperty(string $property): bool 'lazyValidation', ]); } + + public function __serialize(): array + { + return $this->jsonSerialize(); + } + + public function __unserialize(array $data): void + { + $this->__construct($data); + } } From e65a82fc5a27d373396bab20b7570c2e64d8c081 Mon Sep 17 00:00:00 2001 From: Patrick Thomas Date: Mon, 10 Feb 2025 19:12:10 +0100 Subject: [PATCH 2/5] Fixing issue 100 - Exception: Serialization of 'Closure' is not allowed #100 --- src/SimpleDTO.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/SimpleDTO.php b/src/SimpleDTO.php index aac5285..2634282 100644 --- a/src/SimpleDTO.php +++ b/src/SimpleDTO.php @@ -95,6 +95,16 @@ public function __get(string $name): mixed return $this->{$name} ?? null; } + public function __serialize(): array + { + return $this->jsonSerialize(); + } + + public function __unserialize(array $data): void + { + $this->__construct($data); + } + /** * Defines the default values for the properties of the DTO. */ @@ -559,14 +569,4 @@ private function isforbiddenProperty(string $property): bool 'lazyValidation', ]); } - - public function __serialize(): array - { - return $this->jsonSerialize(); - } - - public function __unserialize(array $data): void - { - $this->__construct($data); - } } From 03cecdc0a514fd01e4a2a1fc8de18bd1f556c757 Mon Sep 17 00:00:00 2001 From: Patrick Thomas Date: Mon, 17 Feb 2025 09:14:46 +0100 Subject: [PATCH 3/5] Added libraries to docker image --- docker/Dockerfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docker/Dockerfile b/docker/Dockerfile index 53732e7..0c00870 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,6 +4,19 @@ FROM php:8.2-cli-alpine COPY --from=composer /usr/bin/composer /usr/bin/composer RUN composer self-update +# Install system dependencies +RUN apk add --no-cache --update \ + curl \ + openssl \ + libpng-dev \ + libjpeg-turbo-dev \ + freetype-dev \ + libzip-dev \ + unzip + +# Install the GD extension +RUN docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install gd + WORKDIR /usr/src/app COPY . . From 134185c5653724747490ec6ab5f1d9d12e3e6920 Mon Sep 17 00:00:00 2001 From: Patrick Thomas Date: Mon, 17 Feb 2025 09:28:02 +0100 Subject: [PATCH 4/5] Added test --- tests/Unit/SimpleDTOTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Unit/SimpleDTOTest.php b/tests/Unit/SimpleDTOTest.php index 7266c4d..8a19462 100644 --- a/tests/Unit/SimpleDTOTest.php +++ b/tests/Unit/SimpleDTOTest.php @@ -102,6 +102,22 @@ ->toBe(['name' => $this->subject_name]); }); +it('validates that a SimpleDTO can be instantiated from an Eloquent Model and also get serialized', function () { + $model = new class() extends Model + { + protected $fillable = ['name']; + }; + + $model->fill(['name' => $this->subject_name]); + + $simpleDTO = SimpleDTOInstance::fromModel($model); + + $serialized = unserialize(serialize($simpleDTO)); + + expect($serialized->validatedData) + ->toBe(['name' => $this->subject_name]); +}); + it('validates that a SimpleDTO can be instantiated from Command arguments', function () { $command = new class() extends Command { From cc671b773b42879b11e133acbbb0287cade04803 Mon Sep 17 00:00:00 2001 From: Patrick Thomas Date: Mon, 17 Feb 2025 09:33:57 +0100 Subject: [PATCH 5/5] Added doc block --- src/SimpleDTO.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SimpleDTO.php b/src/SimpleDTO.php index 2634282..515b29b 100644 --- a/src/SimpleDTO.php +++ b/src/SimpleDTO.php @@ -100,6 +100,9 @@ public function __serialize(): array return $this->jsonSerialize(); } + /** + * @throws ValidationException|MissingCastTypeException|CastTargetException + */ public function __unserialize(array $data): void { $this->__construct($data);