RecipientResponseData.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Data\Pagarme\Response\RecipientResponseData;
  3. use App\Data\Pagarme\PagarmeResponseData;
  4. final readonly class RecipientResponseData extends PagarmeResponseData
  5. {
  6. public function __construct(
  7. public ?string $id,
  8. public ?string $name,
  9. public ?string $email,
  10. public ?string $document,
  11. public ?string $type,
  12. public ?string $status,
  13. public ?RecipientBankAccountResponseData $defaultBankAccount = null,
  14. public ?string $createdAt = null,
  15. public ?string $updatedAt = null,
  16. ) {}
  17. public function requireId(): string
  18. {
  19. if (! $this->id) {
  20. throw new \RuntimeException('Pagar.me recipient creation returned an empty id.');
  21. }
  22. return $this->id;
  23. }
  24. //
  25. public static function fromArray(array $payload): static
  26. {
  27. $bankAccount = static::arrArray($payload, 'default_bank_account');
  28. return new self(
  29. id: static::arrString($payload, 'id'),
  30. name: static::arrString($payload, 'name'),
  31. email: static::arrString($payload, 'email'),
  32. document: static::arrString($payload, 'document'),
  33. type: static::arrString($payload, 'type'),
  34. status: static::arrString($payload, 'status'),
  35. defaultBankAccount: ! empty($bankAccount)
  36. ? RecipientBankAccountResponseData::fromArray($bankAccount)
  37. : null,
  38. createdAt: static::arrString($payload, 'created_at'),
  39. updatedAt: static::arrString($payload, 'updated_at'),
  40. );
  41. }
  42. public function toArray(): array
  43. {
  44. return [
  45. 'id' => $this->id,
  46. 'name' => $this->name,
  47. 'email' => $this->email,
  48. 'document' => $this->document,
  49. 'type' => $this->type,
  50. 'status' => $this->status,
  51. 'default_bank_account' => $this->defaultBankAccount?->toArray(),
  52. 'created_at' => $this->createdAt,
  53. 'updated_at' => $this->updatedAt,
  54. ];
  55. }
  56. }