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.
10 class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
14 * Bool indicating whether or not negative values are allowed
16 protected $negative = true;
19 * Bool indicating whether or not zero is allowed
21 protected $zero = true;
24 * Bool indicating whether or not positive values are allowed
26 protected $positive = true;
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
33 public function __construct(
34 $negative = true, $zero = true, $positive = true
36 $this->negative = $negative;
38 $this->positive = $positive;
41 public function validate($integer, $config, $context) {
43 $integer = $this->parseCDATA($integer);
44 if ($integer === '') return false;
46 // we could possibly simply typecast it to integer, but there are
47 // certain fringe cases that must not return an integer.
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
59 // test if it's numeric
60 if (!ctype_digit($digits)) return false;
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;