BulkAnticipationResponseData.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Data\Pagarme\Response;
  3. use App\Data\Pagarme\PagarmeResponseData;
  4. final readonly class BulkAnticipationResponseData extends PagarmeResponseData
  5. {
  6. public function __construct(
  7. public ?string $id,
  8. public ?string $status,
  9. public ?int $amount,
  10. public ?int $fee,
  11. public ?int $fraudCoverageFee,
  12. public ?int $anticipationFee,
  13. public ?bool $automaticTransfer,
  14. public ?string $type,
  15. public ?string $timeframe,
  16. public ?string $paymentDate,
  17. public ?string $createdAt = null,
  18. public ?string $updatedAt = null,
  19. public mixed $anticipationTax = null,
  20. ) {}
  21. public function requireId(): string
  22. {
  23. if (! $this->id) {
  24. throw new \RuntimeException('Pagar.me bulk anticipation creation returned an empty id.');
  25. }
  26. return $this->id;
  27. }
  28. public static function fromArray(array $payload): static
  29. {
  30. return new self(
  31. id: static::arrString($payload, 'id'),
  32. status: static::arrString($payload, 'status'),
  33. amount: static::arrInt($payload, 'amount'),
  34. fee: static::arrInt($payload, 'fee'),
  35. fraudCoverageFee: static::arrInt($payload, 'fraud_coverage_fee'),
  36. anticipationFee: static::arrInt($payload, 'anticipation_fee'),
  37. automaticTransfer: static::arrBool($payload, 'automatic_transfer'),
  38. type: static::arrString($payload, 'type'),
  39. timeframe: static::arrString($payload, 'timeframe'),
  40. paymentDate: static::arrString($payload, 'payment_date'),
  41. createdAt: static::arrString($payload, 'created_at'),
  42. updatedAt: static::arrString($payload, 'updated_at'),
  43. anticipationTax: static::arrGet($payload, 'anticipation_tax'),
  44. );
  45. }
  46. public function toArray(): array
  47. {
  48. return array_filter([
  49. 'id' => $this->id,
  50. 'status' => $this->status,
  51. 'amount' => $this->amount,
  52. 'fee' => $this->fee,
  53. 'fraud_coverage_fee' => $this->fraudCoverageFee,
  54. 'anticipation_fee' => $this->anticipationFee,
  55. 'automatic_transfer' => $this->automaticTransfer,
  56. 'type' => $this->type,
  57. 'timeframe' => $this->timeframe,
  58. 'payment_date' => $this->paymentDate,
  59. 'created_at' => $this->createdAt,
  60. 'updated_at' => $this->updatedAt,
  61. 'anticipation_tax' => $this->anticipationTax,
  62. ], static fn ($v) => $v !== null);
  63. }
  64. }