DiditData.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Data\Didit;
  3. abstract readonly class DiditData
  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. }