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