Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / Integer.php
blob822ed6770822f4004e76cf883c9d8aa56d66acc2
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
5 /**
6 * Validates an integer.
7 * @note While this class was modeled off the CSS definition, no currently
8 * allowed CSS uses this type. The properties that do are: widows,
9 * orphans, z-index, counter-increment, counter-reset. Some of the
10 * HTML attributes, however, find use for a non-negative version of this.
12 class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
15 /**
16 * Bool indicating whether or not negative values are allowed
18 var $negative = true;
20 /**
21 * Bool indicating whether or not zero is allowed
23 var $zero = true;
25 /**
26 * Bool indicating whether or not positive values are allowed
28 var $positive = true;
30 /**
31 * @param $negative Bool indicating whether or not negative values are allowed
32 * @param $zero Bool indicating whether or not zero is allowed
33 * @param $positive Bool indicating whether or not positive values are allowed
35 function HTMLPurifier_AttrDef_Integer(
36 $negative = true, $zero = true, $positive = true
37 ) {
38 $this->negative = $negative;
39 $this->zero = $zero;
40 $this->positive = $positive;
43 function validate($integer, $config, &$context) {
45 $integer = $this->parseCDATA($integer);
46 if ($integer === '') return false;
48 // we could possibly simply typecast it to integer, but there are
49 // certain fringe cases that must not return an integer.
51 // clip leading sign
52 if ( $this->negative && $integer[0] === '-' ) {
53 $digits = substr($integer, 1);
54 if ($digits === '0') $integer = '0'; // rm minus sign for zero
55 } elseif( $this->positive && $integer[0] === '+' ) {
56 $digits = $integer = substr($integer, 1); // rm unnecessary plus
57 } else {
58 $digits = $integer;
61 // test if it's numeric
62 if (!ctype_digit($digits)) return false;
64 // perform scope tests
65 if (!$this->zero && $integer == 0) return false;
66 if (!$this->positive && $integer > 0) return false;
67 if (!$this->negative && $integer < 0) return false;
69 return $integer;