PagarmeRecipientResponseData.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 function defaultBankAccount(): ?PagarmeRecipientBankAccountResponseData
  17. {
  18. return $this->defaultBankAccount;
  19. }
  20. public function id(): ?string
  21. {
  22. return $this->id;
  23. }
  24. public function requireId(): string
  25. {
  26. if (! $this->id) {
  27. throw new \RuntimeException('Pagar.me recipient creation returned an empty id.');
  28. }
  29. return $this->id;
  30. }
  31. //
  32. public static function fromArray(array $payload): self
  33. {
  34. return new self(
  35. id: $payload['id'] ?? null,
  36. name: $payload['name'] ?? null,
  37. email: $payload['email'] ?? null,
  38. document: $payload['document'] ?? null,
  39. type: $payload['type'] ?? null,
  40. status: $payload['status'] ?? null,
  41. defaultBankAccount: ! empty($payload['default_bank_account'])
  42. ? PagarmeRecipientBankAccountResponseData::fromArray($payload['default_bank_account'])
  43. : null,
  44. createdAt: $payload['created_at'] ?? null,
  45. updatedAt: $payload['updated_at'] ?? null,
  46. );
  47. }
  48. public function toArray(): array
  49. {
  50. return [
  51. 'id' => $this->id,
  52. 'name' => $this->name,
  53. 'email' => $this->email,
  54. 'document' => $this->document,
  55. 'type' => $this->type,
  56. 'status' => $this->status,
  57. 'default_bank_account' => $this->defaultBankAccount?->toArray(),
  58. 'created_at' => $this->createdAt,
  59. 'updated_at' => $this->updatedAt,
  60. ];
  61. }
  62. }