PagarmeData.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Data\Pagarme;
  3. abstract readonly class PagarmeData
  4. {
  5. abstract public function toArray(): array;
  6. protected static function digits(?string $value): string
  7. {
  8. return preg_replace('/\D+/', '', (string) $value) ?? '';
  9. }
  10. protected function filterFilledRecursive(array $data): array
  11. {
  12. $filtered = [];
  13. foreach ($data as $key => $value) {
  14. if ($value instanceof self) {
  15. $value = $value->toArray();
  16. }
  17. if (is_array($value)) {
  18. $value = $this->filterFilledRecursive($value);
  19. }
  20. if ($value !== null && $value !== '' && $value !== []) {
  21. $filtered[$key] = $value;
  22. }
  23. }
  24. return $filtered;
  25. }
  26. protected static function requireFilled(mixed $value, string $field): void
  27. {
  28. if ($value === null || $value === '' || $value === []) {
  29. throw new \InvalidArgumentException("{$field} e obrigatorio.");
  30. }
  31. }
  32. protected static function requireIn(string $value, array $allowed, string $field): void
  33. {
  34. if (! in_array($value, $allowed, true)) {
  35. throw new \InvalidArgumentException("{$field} invalido.");
  36. }
  37. }
  38. protected static function requirePositiveInt(int $value, string $field): void
  39. {
  40. if ($value <= 0) {
  41. throw new \InvalidArgumentException("{$field} deve ser maior que zero.");
  42. }
  43. }
  44. }