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 . . diff --git a/src/SimpleDTO.php b/src/SimpleDTO.php index 4f39567..515b29b 100644 --- a/src/SimpleDTO.php +++ b/src/SimpleDTO.php @@ -95,6 +95,19 @@ public function __get(string $name): mixed return $this->{$name} ?? null; } + public function __serialize(): array + { + return $this->jsonSerialize(); + } + + /** + * @throws ValidationException|MissingCastTypeException|CastTargetException + */ + public function __unserialize(array $data): void + { + $this->__construct($data); + } + /** * Defines the default values for the properties of the DTO. */ 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 {