PagarmeCardResponseData.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Data\Pagarme\Response;
  3. final readonly class PagarmeCardResponseData extends PagarmeResponseData
  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 requireId(): string
  19. {
  20. if (! $this->id) {
  21. throw new \RuntimeException('Pagar.me card creation returned an empty id.');
  22. }
  23. return $this->id;
  24. }
  25. //
  26. public static function fromArray(array $payload): static
  27. {
  28. return new self(
  29. id: static::arrString($payload, 'id'),
  30. firstSixDigits: static::arrString($payload, 'first_six_digits'),
  31. lastFourDigits: static::arrString($payload, 'last_four_digits'),
  32. brand: static::arrString($payload, 'brand'),
  33. holderName: static::arrString($payload, 'holder_name'),
  34. expMonth: static::arrInt($payload, 'exp_month'),
  35. expYear: static::arrInt($payload, 'exp_year'),
  36. status: static::arrString($payload, 'status'),
  37. type: static::arrString($payload, 'type'),
  38. createdAt: static::arrString($payload, 'created_at'),
  39. updatedAt: static::arrString($payload, 'updated_at'),
  40. );
  41. }
  42. public function toArray(): array
  43. {
  44. return [
  45. 'id' => $this->id,
  46. 'first_six_digits' => $this->firstSixDigits,
  47. 'last_four_digits' => $this->lastFourDigits,
  48. 'brand' => $this->brand,
  49. 'holder_name' => $this->holderName,
  50. 'exp_month' => $this->expMonth,
  51. 'exp_year' => $this->expYear,
  52. 'status' => $this->status,
  53. 'type' => $this->type,
  54. 'created_at' => $this->createdAt,
  55. 'updated_at' => $this->updatedAt,
  56. ];
  57. }
  58. }