CustomerResponseData.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Data\Pagarme\Response\CustomerResponseData;
  3. use App\Data\Pagarme\Response\CustomerResponseData\CustomerPhonesResponseData\CustomerPhonesResponseData;
  4. use App\Data\Pagarme\PagarmeResponseData;
  5. final readonly class CustomerResponseData extends PagarmeResponseData
  6. {
  7. public function __construct(
  8. public ?string $id,
  9. public ?string $name,
  10. public ?string $email,
  11. public ?string $code,
  12. public ?string $document,
  13. public ?string $documentType,
  14. public ?string $type,
  15. public ?bool $delinquent,
  16. public ?CustomerAddressResponseData $address, public CustomerPhonesResponseData $phones,
  17. public ?string $createdAt = null,
  18. public ?string $updatedAt = null,
  19. ) {}
  20. public function requireId(): string
  21. {
  22. if (! $this->id) {
  23. throw new \RuntimeException('Customer creation returned an empty id.');
  24. }
  25. return $this->id;
  26. }
  27. //
  28. public static function fromArray(array $payload): static
  29. {
  30. $address = static::arrArray($payload, 'address');
  31. $phones = static::arrArray($payload, 'phones');
  32. return new self(
  33. id: static::arrString($payload, 'id'),
  34. name: static::arrString($payload, 'name'),
  35. email: static::arrString($payload, 'email'),
  36. code: static::arrString($payload, 'code'),
  37. document: static::arrString($payload, 'document'),
  38. documentType: static::arrString($payload, 'document_type'),
  39. type: static::arrString($payload, 'type'),
  40. delinquent: static::arrBool($payload, 'delinquent'),
  41. address: ! empty($address) ? CustomerAddressResponseData::fromArray($address) : null,
  42. phones: CustomerPhonesResponseData::fromArray($phones),
  43. createdAt: static::arrString($payload, 'created_at'),
  44. updatedAt: static::arrString($payload, 'updated_at'),
  45. );
  46. }
  47. public function toArray(): array
  48. {
  49. return [
  50. 'id' => $this->id,
  51. 'name' => $this->name,
  52. 'email' => $this->email,
  53. 'code' => $this->code,
  54. 'document' => $this->document,
  55. 'document_type' => $this->documentType,
  56. 'type' => $this->type,
  57. 'delinquent' => $this->delinquent,
  58. 'address' => $this->address?->toArray(),
  59. 'phones' => $this->phones->toArray(),
  60. 'created_at' => $this->createdAt,
  61. 'updated_at' => $this->updatedAt,
  62. ];
  63. }
  64. }