PermissionDTO.php 803 B

123456789101112131415161718192021222324252627282930313233343536
  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(...$request->validated());
  16. }
  17. public function toArray(): array
  18. {
  19. return get_object_vars($this);
  20. }
  21. public static function fromArray(array $data): self
  22. {
  23. return new self(
  24. scope: $data['scope'],
  25. description: $data['description'],
  26. bits: $data['bits'],
  27. parent_id: isset($data['parent_id']) ? $data['parent_id'] : null,
  28. );
  29. }
  30. }