PagarmeCustomerResponseData.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Data\Pagarme\Response\PagarmeCustomerResponseData;
  3. use App\Data\Pagarme\Response\PagarmeCustomerResponseData\PagarmeCustomerPhonesResponseData\PagarmeCustomerPhonesResponseData;
  4. final readonly class PagarmeCustomerResponseData
  5. {
  6. public function __construct(
  7. public ?string $id,
  8. public ?string $name,
  9. public ?string $email,
  10. public ?string $code,
  11. public ?string $document,
  12. public ?string $documentType,
  13. public ?string $type,
  14. public ?bool $delinquent,
  15. public ?PagarmeCustomerAddressResponseData $address,
  16. public PagarmeCustomerPhonesResponseData $phones,
  17. public ?string $createdAt = null,
  18. public ?string $updatedAt = null,
  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('Customer creation returned an empty id.');
  28. }
  29. return $this->id;
  30. }
  31. public static function fromArray(array $payload): self
  32. {
  33. return new self(
  34. id: $payload['id'] ?? null,
  35. name: $payload['name'] ?? null,
  36. email: $payload['email'] ?? null,
  37. code: $payload['code'] ?? null,
  38. document: $payload['document'] ?? null,
  39. documentType: $payload['document_type'] ?? null,
  40. type: $payload['type'] ?? null,
  41. delinquent: $payload['delinquent'] ?? null,
  42. address: PagarmeCustomerAddressResponseData::fromArray($payload['address'] ?? null),
  43. phones: PagarmeCustomerPhonesResponseData::fromArray($payload['phones'] ?? 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. 'code' => $this->code,
  55. 'document' => $this->document,
  56. 'document_type' => $this->documentType,
  57. 'type' => $this->type,
  58. 'delinquent' => $this->delinquent,
  59. 'address' => $this->address?->toArray(),
  60. 'phones' => $this->phones->toArray(),
  61. 'created_at' => $this->createdAt,
  62. 'updated_at' => $this->updatedAt,
  63. ];
  64. }
  65. }