Yes.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 function is_string;
  9. use function nl_langinfo;
  10. use function preg_match;
  11. use const YESEXPR;
  12. /**
  13. * Validates if the input considered as "Yes".
  14. *
  15. * @author Cameron Hall <me@chall.id.au>
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. */
  18. final class Yes extends AbstractRule
  19. {
  20. /**
  21. * @var bool
  22. */
  23. private $useLocale;
  24. /**
  25. * Initializes the rule.
  26. */
  27. public function __construct(bool $useLocale = false)
  28. {
  29. $this->useLocale = $useLocale;
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function validate($input): bool
  35. {
  36. if (!is_string($input)) {
  37. return false;
  38. }
  39. return preg_match($this->getPattern(), $input) > 0;
  40. }
  41. private function getPattern(): string
  42. {
  43. if ($this->useLocale) {
  44. return '/' . nl_langinfo(YESEXPR) . '/';
  45. }
  46. return '/^y(eah?|ep|es)?$/i';
  47. }
  48. }