RecipientResponseData.php 2.2 KB

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