Kaynağa Gözat

feat: :sparkles: creating a BaseDTO to abstract the fucntions and implement DRY

Denis 1 yıl önce
ebeveyn
işleme
e6c08d9568

+ 1 - 6
app/DTO/AuthDTO.php

@@ -4,7 +4,7 @@
 
 use App\Http\Requests\AuthRequest;
 
-readonly class AuthDTO
+readonly class AuthDTO extends BaseDTO
 {
     public function __construct(
         public string $email,
@@ -16,9 +16,4 @@ public static function fromRequest(AuthRequest $request): self
     {
         return new self(...$request->validated());
     }
-
-    public function toArray(): array
-    {
-        return get_object_vars($this);
-    }
 }

+ 47 - 0
app/DTO/BaseDTO.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\DTO;
+
+use ReflectionClass;
+use JsonSerializable;
+use Illuminate\Contracts\Support\Arrayable;
+
+readonly class BaseDTO implements Arrayable, JsonSerializable
+{
+    /**
+     * Create a new DTO from an array of data.
+     */
+    public static function fromArray(array $data): static
+    {
+        $reflection = new ReflectionClass(static::class);
+        $constructor = $reflection->getConstructor();
+
+        if (!$constructor) {
+            throw new \RuntimeException('DTO must have a constructor');
+        }
+
+        $properties = [];
+        foreach ($constructor->getParameters() as $parameter) {
+            $properties[$parameter->getName()] = $data[$parameter->getName()]
+                ?? ($parameter->isOptional() ? $parameter->getDefaultValue() : null);
+        }
+
+        return new static(...$properties);
+    }
+
+    /**
+     * Convert the DTO to an array.
+     */
+    public function toArray(): array
+    {
+        return get_object_vars($this);
+    }
+
+    /**
+     * Convert the DTO to JSON.
+     */
+    public function jsonSerialize(): array
+    {
+        return $this->toArray();
+    }
+}

+ 2 - 18
app/DTO/PermissionDTO.php

@@ -4,33 +4,17 @@
 
 use App\Http\Requests\PermissionRequest;
 
-readonly class PermissionDTO
+readonly class PermissionDTO extends BaseDTO
 {
     public function __construct(
         public string $scope,
         public string $description,
         public string $bits,
         public ?string $parent_id,
-    ) {
-    }
+    ) {}
 
     public static function fromRequest(PermissionRequest $request): self
     {
         return new self(...$request->validated());
     }
-
-    public function toArray(): array
-    {
-        return get_object_vars($this);
-    }
-
-    public static function fromArray(array $data): self
-    {
-        return new self(
-            scope: $data['scope'],
-            description: $data['description'],
-            bits: $data['bits'],
-            parent_id: isset($data['parent_id']) ? $data['parent_id'] : null,
-        );
-    }
 }

+ 1 - 6
app/DTO/RefreshTokenDTO.php

@@ -4,7 +4,7 @@
 
 use App\Http\Requests\RefreshTokenRequest;
 
-readonly class RefreshTokenDTO
+readonly class RefreshTokenDTO extends BaseDTO
 {
     public function __construct(
         public ?string $refresh_token = null,
@@ -15,9 +15,4 @@ public static function fromRequest(RefreshTokenRequest $request): self
     {
         return new self(...$request->validated());
     }
-
-    public function toArray(): array
-    {
-        return get_object_vars($this);
-    }
 }

+ 1 - 6
app/DTO/UserDTO.php

@@ -4,7 +4,7 @@
 
 use App\Http\Requests\UserRequest;
 
-readonly class UserDTO
+readonly class UserDTO extends BaseDTO
 {
     public function __construct(
         public ?string $name = null,
@@ -18,9 +18,4 @@ public static function fromRequest(UserRequest $request): self
     {
         return new self(...$request->validated());
     }
-
-    public function toArray(): array
-    {
-        return get_object_vars($this);
-    }
 }

+ 1 - 6
storage/stubs/DTO.stub

@@ -4,7 +4,7 @@ namespace App\DTO;
 
 use App\Http\Requests\{{modelName}}Request;
 
-readonly class {{modelName}}DTO
+readonly class {{modelName}}DTO extends BaseDTO
 {
     public function __construct(
         // Add properties here
@@ -15,9 +15,4 @@ readonly class {{modelName}}DTO
     {
         return new self(...$request->validated());
     }
-
-    public function toArray(): array
-    {
-        return get_object_vars($this);
-    }
 }