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 IČ (Czech company number).
16: *
17: * Taken from David Grudl's http://latrine.dgx.cz/jak-overit-platne-ic-a-rodne-cislo
18: *
19: * @category Jyxo
20: * @package Jyxo_Input
21: * @subpackage Validator
22: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
23: * @license https://github.com/jyxo/php/blob/master/license.txt
24: * @author Jaroslav Hanslík
25: */
26: class Jyxo_Input_Validator_IsCompanyId extends Jyxo_Input_Validator_AbstractValidator
27: {
28: /**
29: * Validates a value.
30: *
31: * @param mixed $value Input value
32: * @return boolean
33: */
34: public function isValid($value)
35: {
36: // Removes spaces
37: $companyId = preg_replace('~\s+~', '', (string) $value);
38:
39: // Only numbers
40: if (!preg_match('~^\d{8}$~', $companyId)) {
41: return false;
42: }
43:
44: // Checksum
45: $a = 0;
46: for ($i = 0; $i < 7; $i++) {
47: $a += $companyId[$i] * (8 - $i);
48: }
49:
50: $a = $a % 11;
51: if (0 === $a) {
52: $c = 1;
53: } elseif (10 === $a) {
54: $c = 1;
55: } elseif (1 === $a) {
56: $c = 0;
57: } else {
58: $c = 11 - $a;
59: }
60:
61: if ((int) $companyId[7] !== $c) {
62: return false;
63: }
64:
65: return true;
66: }
67: }
68: