Overview

Namespaces

  • MvcCore
    • Ext
      • Auth
        • Virtual
      • Debug
        • Tracy
      • Form
        • Core
        • Validators
      • Request
      • Router
        • Lang
      • View
        • Helpers
  • None

Classes

  • MvcCore
  • MvcCore\Config
  • MvcCore\Controller
  • MvcCore\Debug
  • MvcCore\Ext\Auth
  • MvcCore\Ext\Auth\Controller
  • MvcCore\Ext\Auth\SignInForm
  • MvcCore\Ext\Auth\SignOutForm
  • MvcCore\Ext\Auth\User
  • MvcCore\Ext\Auth\Virtual\Controller
  • MvcCore\Ext\Auth\Virtual\Form
  • MvcCore\Ext\Auth\Virtual\User
  • MvcCore\Ext\Debug\Tracy
  • MvcCore\Ext\Debug\Tracy\AuthPanel
  • MvcCore\Ext\Debug\Tracy\IncludePanel
  • MvcCore\Ext\Debug\Tracy\MvcCorePanel
  • MvcCore\Ext\Debug\Tracy\RoutingPanel
  • MvcCore\Ext\Debug\Tracy\SessionPanel
  • MvcCore\Ext\Form
  • MvcCore\Ext\Form\Button
  • MvcCore\Ext\Form\Checkbox
  • MvcCore\Ext\Form\CheckboxGroup
  • MvcCore\Ext\Form\Core\Base
  • MvcCore\Ext\Form\Core\Configuration
  • MvcCore\Ext\Form\Core\Field
  • MvcCore\Ext\Form\Core\FieldGroup
  • MvcCore\Ext\Form\Core\Helpers
  • MvcCore\Ext\Form\Core\Validator
  • MvcCore\Ext\Form\Core\View
  • MvcCore\Ext\Form\CountrySelect
  • MvcCore\Ext\Form\Date
  • MvcCore\Ext\Form\DateTime
  • MvcCore\Ext\Form\Email
  • MvcCore\Ext\Form\Hidden
  • MvcCore\Ext\Form\NoType
  • MvcCore\Ext\Form\Number
  • MvcCore\Ext\Form\Password
  • MvcCore\Ext\Form\RadioGroup
  • MvcCore\Ext\Form\Range
  • MvcCore\Ext\Form\ResetButton
  • MvcCore\Ext\Form\ResetInput
  • MvcCore\Ext\Form\Select
  • MvcCore\Ext\Form\SubmitButton
  • MvcCore\Ext\Form\SubmitInput
  • MvcCore\Ext\Form\Text
  • MvcCore\Ext\Form\Textarea
  • MvcCore\Ext\Form\Time
  • MvcCore\Ext\Form\Validators\CompanyId
  • MvcCore\Ext\Form\Validators\CompanyVatId
  • MvcCore\Ext\Form\Validators\Date
  • MvcCore\Ext\Form\Validators\Email
  • MvcCore\Ext\Form\Validators\FloatVal
  • MvcCore\Ext\Form\Validators\Integer
  • MvcCore\Ext\Form\Validators\Maxlength
  • MvcCore\Ext\Form\Validators\MaxSelectedOptions
  • MvcCore\Ext\Form\Validators\MinSelectedOptions
  • MvcCore\Ext\Form\Validators\NumberField
  • MvcCore\Ext\Form\Validators\Pattern
  • MvcCore\Ext\Form\Validators\Phone
  • MvcCore\Ext\Form\Validators\RangeField
  • MvcCore\Ext\Form\Validators\SafeString
  • MvcCore\Ext\Form\Validators\Time
  • MvcCore\Ext\Form\Validators\Url
  • MvcCore\Ext\Form\Validators\ValueInOptions
  • MvcCore\Ext\Form\Validators\ZipCode
  • MvcCore\Ext\Request\ApacheDpi
  • MvcCore\Ext\Request\Cli
  • MvcCore\Ext\Router\Lang
  • MvcCore\Ext\Router\Lang\Route
  • MvcCore\Ext\Router\Media
  • MvcCore\Ext\Router\MediaSiteKey
  • MvcCore\Ext\View\Helpers\Assets
  • MvcCore\Ext\View\Helpers\Css
  • MvcCore\Ext\View\Helpers\Js
  • MvcCore\Ext\View\Helpers\LineBreaks
  • MvcCore\Model
  • MvcCore\Request
  • MvcCore\Response
  • MvcCore\Route
  • MvcCore\Router
  • MvcCore\Session
  • MvcCore\Tool
  • MvcCore\View

Exceptions

  • MvcCore\Ext\Form\Core\Exception
  • Overview
  • Namespace
  • Class
  • Tree
  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

/**
 * MvcCore
 *
 * This source file is subject to the BSD 3 License
 * For the full copyright and license information, please view 
 * the LICENSE.md file that are distributed with this source code.
 *
 * @copyright   Copyright (c) 2016 Tom FlĂ­dr (https://github.com/mvccore/mvccore)
 * @license     https://mvccore.github.io/docs/mvccore/4.0.0/LICENCE.md
 */

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
{
    /**
     * Error message key
     * @var string
     */
    protected static $errorMessageKey = \MvcCore\Ext\Form::TAX_ID;
    /**
     * EU validators
     * Array of regexp bases to check company id.
     * If item is array of regexp bases, company id is 
     * checked in or behaviour. If item is closure function,
     * company id is checked by calling closure function
     * with fist param to be company id. Closure function has
     * to return boolean about company id right form.
     * Add any other custom validator by info bellow:
     * @see https://en.wikipedia.org/wiki/VAT_identification_number
     * @var array
     */
    public static $Validators = array();
    /**
     * Static initialization for this class to init validators.
     * @return void
     */
    public function StaticInit () {
        self::$Validators = array(
            'AT'=> 'U(\d{8})',                      // Austria
            'BE'=> '(0?\d{9})',                 // Belgium
            'BG'=> '(\d{9,10})',                    // Bulgaria
            'CHE'=>'(\d{9})(MWST)?',                // Switzerland
            'CY'=> '([0-5|9]\d{7}[A-Z])',           // Cyprus
            'CZ'=> function ($id = '') {            // Czech republic
                $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})',                  // Germany
            'DK'=> '(\d{8})',                       // Denmark
            'EL'=> '(10\d{7})',                     // Estonia
            'ES'=> array(                           // Spain
                '([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})',                       // EU type
            'FI'=> '(\d{8})',                       // Finland
            'FR'=> array(                           // France
                '(\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(                           // Great Britain
                '?(\d{9})',
                '?(\d{12})',
                '?(GD\d{3})',
                '?(HA\d{3})',
            ),
            'GR'=> '(\d{8,9})',                     // Greece
            'HR'=> '(\d{11})',                      // Croatia
            'HU'=> '(\d{8})',                       // Hungary
            'IE'=> array(                           // Ireland
                '(\d{7}[A-W])',
                '([7-9][A-Z\*\+)]\d{5}[A-W])',
                '(\d{7}[A-W][AH])',
            ),
            'IT'=> '(\d{11})',                      // Italy
            'LV'=> '(\d{11})',                      // Latvia
            'LT'=> '(\d{9}|\d{12})',                // Lithunia
            'LU'=> '(\d{8})',                       // Luxembourg
            'MT'=> '([1-9]\d{7})',                  // Malta
            'NL'=> '(\d{9})B\d{2}',                 // Netherland
            'NO'=> '(\d{9})',                       // Norway
            'PL'=> '(\d{10})',                      // Poland
            'PT'=> '(\d{9})',                       // Portugal
            'RO'=> '([1-9]\d{1,9})',                // Romania
            'RS'=> '(\d{9})',                       // Serbia
            'SI'=> '([1-9]\d{7})',                  // Slovenia
            'SK'=> '([1-9]\d[(2-4)|(6-9)]\d{7})',   // Slovak republic
            'SE'=> '(\d{10}01)',                    // Sweden
        );
    }
    /**
     * Validate company ID by regular expression(s) or by closure function.
     * @param string                $submitValue    raw submitted value
     * @param string                $fieldName      form field name
     * @param \MvcCore\Ext\Form\Core\Field $field           form field for company id
     * @throws \MvcCore\Ext\Form\Core\Exception 
     * @return mixed
     */
    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;
    }
    /**
     * Check company ID by regular expression base.
     * Return true if company ID matches.
     * @param string $regExpBase
     * @param string $locale
     * @param string $id 
     * @return bool
     */
    protected function checkCompanyIdByRegExpBase ($regExpBase, $locale, $id) {
        preg_match("#^$regExpBase$#", $id, $matches);
        return count($matches) > 0;
    }
}
MvcCore API documentation generated by ApiGen