MacAddress.php 820 B

123456789101112131415161718192021222324252627282930313233343536
  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 preg_match;
  10. /**
  11. * Validates whether the input is a valid MAC address.
  12. *
  13. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  14. * @author Danilo Correa <danilosilva87@gmail.com>
  15. * @author Fábio da Silva Ribeiro <fabiorphp@gmail.com>
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. */
  18. final class MacAddress extends AbstractRule
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function validate($input): bool
  24. {
  25. if (!is_string($input)) {
  26. return false;
  27. }
  28. return preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input) > 0;
  29. }
  30. }