PagarmeRecipientResponseData.php 2.2 KB

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