Cnpj.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 array_map;
  9. use function array_sum;
  10. use function count;
  11. use function is_scalar;
  12. use function preg_replace;
  13. use function str_split;
  14. /**
  15. * Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
  16. *
  17. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  18. * @author Henrique Moody <henriquemoody@gmail.com>
  19. * @author Jayson Reis <santosdosreis@gmail.com>
  20. * @author Nick Lombard <github@jigsoft.co.za>
  21. * @author Renato Moura <renato@naturalweb.com.br>
  22. * @author William Espindola <oi@williamespindola.com.br>
  23. */
  24. final class Cnpj extends AbstractRule
  25. {
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function validate($input): bool
  30. {
  31. if (!is_scalar($input)) {
  32. return false;
  33. }
  34. // Code ported from jsfromhell.com
  35. $bases = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
  36. $digits = $this->getDigits((string) $input);
  37. if (array_sum($digits) < 1) {
  38. return false;
  39. }
  40. if (count($digits) !== 14) {
  41. return false;
  42. }
  43. $n = 0;
  44. for ($i = 0; $i < 12; ++$i) {
  45. $n += $digits[$i] * $bases[$i + 1];
  46. }
  47. if ($digits[12] != (($n %= 11) < 2 ? 0 : 11 - $n)) {
  48. return false;
  49. }
  50. $n = 0;
  51. for ($i = 0; $i <= 12; ++$i) {
  52. $n += $digits[$i] * $bases[$i];
  53. }
  54. $check = ($n %= 11) < 2 ? 0 : 11 - $n;
  55. return $digits[13] == $check;
  56. }
  57. /**
  58. * @return int[]
  59. */
  60. private function getDigits(string $input): array
  61. {
  62. return array_map(
  63. 'intval',
  64. str_split(
  65. (string) preg_replace('/\D/', '', $input)
  66. )
  67. );
  68. }
  69. }