PagarmeCardResponseData.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Data\Pagarme\Response;
  3. final readonly class PagarmeCardResponseData
  4. {
  5. public function __construct(
  6. public ?string $id,
  7. public ?string $firstSixDigits,
  8. public ?string $lastFourDigits,
  9. public ?string $brand,
  10. public ?string $holderName,
  11. public ?int $expMonth,
  12. public ?int $expYear,
  13. public ?string $status,
  14. public ?string $type,
  15. public ?string $createdAt = null,
  16. public ?string $updatedAt = null,
  17. ) {}
  18. public function brand(): ?string
  19. {
  20. return $this->brand;
  21. }
  22. public function id(): ?string
  23. {
  24. return $this->id;
  25. }
  26. public function lastFourDigits(): ?string
  27. {
  28. return $this->lastFourDigits;
  29. }
  30. public function requireId(): string
  31. {
  32. if (! $this->id) {
  33. throw new \RuntimeException('Pagar.me card creation returned an empty id.');
  34. }
  35. return $this->id;
  36. }
  37. //
  38. public static function fromArray(array $payload): self
  39. {
  40. return new self(
  41. id: $payload['id'] ?? null,
  42. firstSixDigits: $payload['first_six_digits'] ?? null,
  43. lastFourDigits: $payload['last_four_digits'] ?? null,
  44. brand: $payload['brand'] ?? null,
  45. holderName: $payload['holder_name'] ?? null,
  46. expMonth: isset($payload['exp_month']) ? (int) $payload['exp_month'] : null,
  47. expYear: isset($payload['exp_year']) ? (int) $payload['exp_year'] : null,
  48. status: $payload['status'] ?? null,
  49. type: $payload['type'] ?? null,
  50. createdAt: $payload['created_at'] ?? null,
  51. updatedAt: $payload['updated_at'] ?? null,
  52. );
  53. }
  54. public function toArray(): array
  55. {
  56. return [
  57. 'id' => $this->id,
  58. 'first_six_digits' => $this->firstSixDigits,
  59. 'last_four_digits' => $this->lastFourDigits,
  60. 'brand' => $this->brand,
  61. 'holder_name' => $this->holderName,
  62. 'exp_month' => $this->expMonth,
  63. 'exp_year' => $this->expYear,
  64. 'status' => $this->status,
  65. 'type' => $this->type,
  66. 'created_at' => $this->createdAt,
  67. 'updated_at' => $this->updatedAt,
  68. ];
  69. }
  70. }