1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
<?php
namespace MvcCore\Ext\Form\Validators;
require_once(__DIR__.'/../../Form.php');
require_once(__DIR__.'/../Core/Validator.php');
require_once(__DIR__.'/../Core/Field.php');
require_once(__DIR__.'/../Core/Exception.php');
require_once(__DIR__.'/../Core/View.php');
use
MvcCore\Ext\Form,
MvcCore\Ext\Form\Core;
class CompanyId extends Core\Validator
{
protected static $errorMessageKey = \MvcCore\Ext\Form::TAX_ID;
public static $Validators = array();
public function StaticInit () {
self::$Validators = array(
'AT'=> 'U(\d{8})',
'BE'=> '(0?\d{9})',
'BG'=> '(\d{9,10})',
'CHE'=>'(\d{9})(MWST)?',
'CY'=> '([0-5|9]\d{7}[A-Z])',
'CZ'=> function ($id = '') {
$id = preg_replace('#\s+#', '', $id);
if (!preg_match('#^\d{8}$#', $id)) return FALSE;
$a = 0;
for ($i = 0; $i < 7; $i++) $a += $id[$i] * (8 - $i);
$a = $a % 11;
if ($a === 0) $c = 1;
elseif ($a === 10) $c = 1;
elseif ($a === 1) $c = 0;
else $c = 11 - $a;
return (int) $id[7] === $c;
},
'DE'=> '([1-9]\d{8})',
'DK'=> '(\d{8})',
'EL'=> '(10\d{7})',
'ES'=> array(
'([A-Z]\d{8})))))',
'([A-H|N-S|W]\d{7}[A-J])',
'([0-9|Y|Z]\d{7}[A-Z])',
'([K|L|M|X]\d{7}[A-Z])',
),
'EU'=> '(\d{9})',
'FI'=> '(\d{8})',
'FR'=> array(
'(\d{11})',
'([(A-H)|(J-N)|(P-Z)]\d{10})',
'(\d[(A-H)|(J-N)|(P-Z)]\d{9})',
'([(A-H)|(J-N)|(P-Z)]{2}\d{9})',
),
'GB'=> array(
'?(\d{9})',
'?(\d{12})',
'?(GD\d{3})',
'?(HA\d{3})',
),
'GR'=> '(\d{8,9})',
'HR'=> '(\d{11})',
'HU'=> '(\d{8})',
'IE'=> array(
'(\d{7}[A-W])',
'([7-9][A-Z\*\+)]\d{5}[A-W])',
'(\d{7}[A-W][AH])',
),
'IT'=> '(\d{11})',
'LV'=> '(\d{11})',
'LT'=> '(\d{9}|\d{12})',
'LU'=> '(\d{8})',
'MT'=> '([1-9]\d{7})',
'NL'=> '(\d{9})B\d{2}',
'NO'=> '(\d{9})',
'PL'=> '(\d{10})',
'PT'=> '(\d{9})',
'RO'=> '([1-9]\d{1,9})',
'RS'=> '(\d{9})',
'SI'=> '([1-9]\d{7})',
'SK'=> '([1-9]\d[(2-4)|(6-9)]\d{7})',
'SE'=> '(\d{10}01)',
);
}
public function Validate ($submitValue, $fieldName, \MvcCore\Ext\Form\Core\Field & $field) {
$submitValue = trim($submitValue);
$safeValue = preg_replace("#[^0-9A-Z\*\+]#", '', strtoupper($submitValue));
$formLocale = strtoupper($this->Form->Locale);
$result = FALSE;
if (!$formLocale) {
throw new Core\Exception(
"[".__CLASS__."] Unable to validate company ID without configured form 'Locale' property. "
. "Use \$form->SetLocale('[A-Z]{2}'); to create proper company ID validator."
);
} else {
$formLocale = strtoupper($formLocale);
if (!isset(static::$Validators[$formLocale])) {
throw new Core\Exception(
"[".__CLASS__."] Unable to create company ID validator for locale '$formLocale'. "
. "Function to check company ID for locale '$formLocale' is not implemented yet. "
. "Use different localization or put custom closure function to validate this field."
);
} else {
$validator = self::$Validators[$formLocale];
if (is_callable($validator)) {
$result = call_user_func($validator, $safeValue);
} else if (is_array($validator)) {
foreach ($validator as $validatorItem) {
if ($this->checkCompanyIdByRegExpBase($validatorItem, $formLocale, $safeValue)) {
$result = TRUE;
break;
}
}
} else if (is_string($validator)) {
$result = $this->checkCompanyIdByRegExpBase($validator, $formLocale, $safeValue);
}
}
}
if ((strlen($safeValue) > 0 && !$result) || strlen($safeValue) !== strlen($submitValue)) {
$this->addError($field, Form::$DefaultMessages[static::$errorMessageKey], function ($msg, $args) {
return Core\View::Format($msg, $args);
});
}
return $safeValue;
}
protected function checkCompanyIdByRegExpBase ($regExpBase, $locale, $id) {
preg_match("#^$regExpBase$#", $id, $matches);
return count($matches) > 0;
}
}