1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Validates (Czech) Tax ID.
16: *
17: * @category Jyxo
18: * @package Jyxo_Input
19: * @subpackage Validator
20: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
21: * @license https://github.com/jyxo/php/blob/master/license.txt
22: * @author Jaroslav Hanslík
23: */
24: class Jyxo_Input_Validator_IsTaxId extends Jyxo_Input_Validator_AbstractValidator
25: {
26: /**
27: * Strict check.
28: *
29: * If turned on, validity of IČ/birth number is also performed. If not, only length is checked.
30: *
31: * @var boolean
32: */
33: private $strict = true;
34:
35: /**
36: * Constructor.
37: *
38: * @param boolean $strict Turns strict checking on or off.
39: */
40: public function __construct($strict = true)
41: {
42: $this->strict = (bool) $strict;
43: }
44:
45: /**
46: * Validates a value.
47: *
48: * @param mixed $value Input value
49: * @return boolean
50: */
51: public function isValid($value)
52: {
53: // Removes spaces
54: $taxId = preg_replace('~\s+~', '', (string) $value);
55:
56: $sub = '';
57: // New Tax ID format since 1st May 2004
58: if (preg_match('~^CZ(\d{8,10})$~', $taxId, $matches)) {
59: $sub = $matches[1];
60: // But to be sure we try the old one as well
61: } elseif (preg_match('~^\d{3}-(\d{8,10})$~', $taxId, $matches)) {
62: $sub = $matches[1];
63: }
64: if (!empty($sub)) {
65: // Strict checking off - allows the so called "own numbers"
66: if (!$this->strict) {
67: return true;
68: }
69:
70: // Checks if it is a valid IČ
71: if (Jyxo_Input_Validator_IsCompanyId::validate($sub)) {
72: return true;
73: }
74:
75: // Checks if it is a valid birth number
76: if (Jyxo_Input_Validator_IsBirthNumber::validate($sub)) {
77: return true;
78: }
79: }
80:
81: return false;
82: }
83: }
84: