PermissionDTO.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\DTO;
  3. use App\Http\Requests\PermissionRequest;
  4. readonly class PermissionDTO
  5. {
  6. public function __construct(
  7. public string $scope,
  8. public string $description,
  9. public string $bits,
  10. public ?string $parent_id,
  11. ) {
  12. }
  13. public static function fromRequest(PermissionRequest $request): self
  14. {
  15. return new self(
  16. scope: $request->validated(key: 'scope'),
  17. description: $request->validated(key: 'description'),
  18. bits: $request->validated(key: 'bits'),
  19. parent_id: $request->validated(key: 'parent_id'),
  20. );
  21. }
  22. public static function fromArray(array $data): self
  23. {
  24. return new self(
  25. scope: $data['scope'],
  26. description: $data['description'],
  27. bits: $data['bits'],
  28. parent_id: isset($data['parent_id']) ? $data['parent_id'] : null,
  29. );
  30. }
  31. public function toArray(): array
  32. {
  33. return [
  34. 'scope' => $this->scope,
  35. 'description' => $this->description,
  36. 'bits' => $this->bits,
  37. 'parent_id' => $this->parent_id,
  38. ];
  39. }
  40. }