CardResponseData.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Data\Pagarme\Response;
  3. use App\Data\Pagarme\PagarmeResponseData;
  4. final readonly class CardResponseData extends PagarmeResponseData
  5. {
  6. public function __construct(
  7. public ?string $id,
  8. public ?string $firstSixDigits,
  9. public ?string $lastFourDigits,
  10. public ?string $brand,
  11. public ?string $holderName,
  12. public ?int $expMonth,
  13. public ?int $expYear,
  14. public ?string $status,
  15. public ?string $type,
  16. public ?string $createdAt = null,
  17. public ?string $updatedAt = null,
  18. ) {}
  19. public function requireId(): string
  20. {
  21. if (! $this->id) {
  22. throw new \RuntimeException('Pagar.me card creation returned an empty id.');
  23. }
  24. return $this->id;
  25. }
  26. //
  27. public static function fromArray(array $payload): static
  28. {
  29. return new self(
  30. id: static::arrString($payload, 'id'),
  31. firstSixDigits: static::arrString($payload, 'first_six_digits'),
  32. lastFourDigits: static::arrString($payload, 'last_four_digits'),
  33. brand: static::arrString($payload, 'brand'),
  34. holderName: static::arrString($payload, 'holder_name'),
  35. expMonth: static::arrInt($payload, 'exp_month'),
  36. expYear: static::arrInt($payload, 'exp_year'),
  37. status: static::arrString($payload, 'status'),
  38. type: static::arrString($payload, 'type'),
  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. 'first_six_digits' => $this->firstSixDigits,
  48. 'last_four_digits' => $this->lastFourDigits,
  49. 'brand' => $this->brand,
  50. 'holder_name' => $this->holderName,
  51. 'exp_month' => $this->expMonth,
  52. 'exp_year' => $this->expYear,
  53. 'status' => $this->status,
  54. 'type' => $this->type,
  55. 'created_at' => $this->createdAt,
  56. 'updated_at' => $this->updatedAt,
  57. ];
  58. }
  59. }