NotBlank.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use stdClass;
  9. use function array_filter;
  10. use function is_array;
  11. use function is_numeric;
  12. use function is_string;
  13. use function trim;
  14. /**
  15. * Validates if the given input is not a blank value (null, zeros, empty strings or empty arrays, recursively).
  16. *
  17. * @author Danilo Correa <danilosilva87@gmail.com>
  18. * @author Henrique Moody <henriquemoody@gmail.com>
  19. */
  20. final class NotBlank extends AbstractRule
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function validate($input): bool
  26. {
  27. if (is_numeric($input)) {
  28. return $input != 0;
  29. }
  30. if (is_string($input)) {
  31. $input = trim($input);
  32. }
  33. if ($input instanceof stdClass) {
  34. $input = (array) $input;
  35. }
  36. if (is_array($input)) {
  37. $input = array_filter($input, __METHOD__);
  38. }
  39. return !empty($input);
  40. }
  41. }