]>
Commit | Line | Data |
---|---|---|
1 | <?php\r | |
2 | \r | |
3 | /**\r | |
4 | * Validates an integer.\r | |
5 | * @note While this class was modeled off the CSS definition, no currently\r | |
6 | * allowed CSS uses this type. The properties that do are: widows,\r | |
7 | * orphans, z-index, counter-increment, counter-reset. Some of the\r | |
8 | * HTML attributes, however, find use for a non-negative version of this.\r | |
9 | */\r | |
10 | class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef\r | |
11 | {\r | |
12 | \r | |
13 | /**\r | |
14 | * Whether or not negative values are allowed.\r | |
15 | * @type bool\r | |
16 | */\r | |
17 | protected $negative = true;\r | |
18 | \r | |
19 | /**\r | |
20 | * Whether or not zero is allowed.\r | |
21 | * @type bool\r | |
22 | */\r | |
23 | protected $zero = true;\r | |
24 | \r | |
25 | /**\r | |
26 | * Whether or not positive values are allowed.\r | |
27 | * @type bool\r | |
28 | */\r | |
29 | protected $positive = true;\r | |
30 | \r | |
31 | /**\r | |
32 | * @param $negative Bool indicating whether or not negative values are allowed\r | |
33 | * @param $zero Bool indicating whether or not zero is allowed\r | |
34 | * @param $positive Bool indicating whether or not positive values are allowed\r | |
35 | */\r | |
36 | public function __construct($negative = true, $zero = true, $positive = true)\r | |
37 | {\r | |
38 | $this->negative = $negative;\r | |
39 | $this->zero = $zero;\r | |
40 | $this->positive = $positive;\r | |
41 | }\r | |
42 | \r | |
43 | /**\r | |
44 | * @param string $integer\r | |
45 | * @param HTMLPurifier_Config $config\r | |
46 | * @param HTMLPurifier_Context $context\r | |
47 | * @return bool|string\r | |
48 | */\r | |
49 | public function validate($integer, $config, $context)\r | |
50 | {\r | |
51 | $integer = $this->parseCDATA($integer);\r | |
52 | if ($integer === '') {\r | |
53 | return false;\r | |
54 | }\r | |
55 | \r | |
56 | // we could possibly simply typecast it to integer, but there are\r | |
57 | // certain fringe cases that must not return an integer.\r | |
58 | \r | |
59 | // clip leading sign\r | |
60 | if ($this->negative && $integer[0] === '-') {\r | |
61 | $digits = substr($integer, 1);\r | |
62 | if ($digits === '0') {\r | |
63 | $integer = '0';\r | |
64 | } // rm minus sign for zero\r | |
65 | } elseif ($this->positive && $integer[0] === '+') {\r | |
66 | $digits = $integer = substr($integer, 1); // rm unnecessary plus\r | |
67 | } else {\r | |
68 | $digits = $integer;\r | |
69 | }\r | |
70 | \r | |
71 | // test if it's numeric\r | |
72 | if (!ctype_digit($digits)) {\r | |
73 | return false;\r | |
74 | }\r | |
75 | \r | |
76 | // perform scope tests\r | |
77 | if (!$this->zero && $integer == 0) {\r | |
78 | return false;\r | |
79 | }\r | |
80 | if (!$this->positive && $integer > 0) {\r | |
81 | return false;\r | |
82 | }\r | |
83 | if (!$this->negative && $integer < 0) {\r | |
84 | return false;\r | |
85 | }\r | |
86 | \r | |
87 | return $integer;\r | |
88 | }\r | |
89 | }\r | |
90 | \r | |
91 | // vim: et sw=4 sts=4\r |