PageRenderTime 45ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Validation/ItValidation.php

http://github.com/cakephp/localized
PHP | 111 lines | 49 code | 14 blank | 48 comment | 6 complexity | 5ffae11f6bdea7c31cbce282ed8334c2 MD5 | raw file
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Italian Localized Validation class. Handles localized validation for Italy.
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org
  14. * @since Localized Plugin v 0.1
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Localized\Validation;
  18. use Cake\Http\Exception\NotImplementedException;
  19. /**
  20. * ItValidation
  21. */
  22. class ItValidation extends LocalizedValidation
  23. {
  24. /**
  25. * Define locale to be used by that localized
  26. * validation set
  27. *
  28. * @var string
  29. */
  30. protected static $validationLocale = 'it_IT';
  31. /**
  32. * Checks a phone number for Italy.
  33. *
  34. * @param string $check The value to check.
  35. * @return bool Success.
  36. */
  37. public static function phone(string $check): bool
  38. {
  39. $pattern = '/^(\d*\-?\ ?\/?\d*)$/';
  40. return (bool)preg_match($pattern, $check);
  41. }
  42. /**
  43. * Checks a postal code for Italy.
  44. *
  45. * @param string $check The value to check.
  46. * @return bool Success.
  47. */
  48. public static function postal(string $check): bool
  49. {
  50. $pattern = '/^\d{5}$/i';
  51. return (bool)preg_match($pattern, $check);
  52. }
  53. /**
  54. * Checks Codice Fiscale for Italy.
  55. *
  56. * @param string $check The value to check.
  57. * @return bool Success.
  58. */
  59. public static function cf(string $check): bool
  60. {
  61. if ((strlen($check) === 11) && preg_match('/\d{11}/', $check)) {
  62. return true;
  63. }
  64. $check = strtoupper($check);
  65. if (strlen($check) !== 16 || !preg_match('/[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]/', $check)) {
  66. return false;
  67. }
  68. $checkOdd = [
  69. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9, '5' => 13,
  70. '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  71. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9, 'F' => 13,
  72. 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21, 'K' => 2, 'L' => 4,
  73. 'M' => 18, 'N' => 20, 'O' => 11, 'P' => 3, 'Q' => 6, 'R' => 8,
  74. 'S' => 12, 'T' => 14, 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25,
  75. 'Y' => 24, 'Z' => 23,
  76. ];
  77. $sum = 0;
  78. for ($i = 1; $i <= 13; $i += 2) {
  79. $sum += is_numeric($check[$i]) ? (int)$check[$i] : ord($check[$i]) - ord('A');
  80. }
  81. for ($i = 0; $i <= 14; $i += 2) {
  82. $sum += $checkOdd[$check[$i]];
  83. }
  84. return chr($sum % 26 + ord('A')) === $check[15];
  85. }
  86. /**
  87. * Checks a country specific identification number.
  88. *
  89. * @param string $check The value to check.
  90. * @throws \Cake\Http\Exception\NotImplementedException Exception
  91. * @return bool Success.
  92. */
  93. public static function personId(string $check): bool
  94. {
  95. throw new NotImplementedException(__d('localized', '%s Not implemented yet.'));
  96. }
  97. }