PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / Integer.php
blob400e707d2f5b4449aa49cf957919b0581fd0c6e1
1 <?php
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
13 /**
14 * Whether or not negative values are allowed.
15 * @type bool
17 protected $negative = true;
19 /**
20 * Whether or not zero is allowed.
21 * @type bool
23 protected $zero = true;
25 /**
26 * Whether or not positive values are allowed.
27 * @type bool
29 protected $positive = true;
31 /**
32 * @param $negative Bool indicating whether or not negative values are allowed
33 * @param $zero Bool indicating whether or not zero is allowed
34 * @param $positive Bool indicating whether or not positive values are allowed
36 public function __construct($negative = true, $zero = true, $positive = true)
38 $this->negative = $negative;
39 $this->zero = $zero;
40 $this->positive = $positive;
43 /**
44 * @param string $integer
45 * @param HTMLPurifier_Config $config
46 * @param HTMLPurifier_Context $context
47 * @return bool|string
49 public function validate($integer, $config, $context)
51 $integer = $this->parseCDATA($integer);
52 if ($integer === '') {
53 return false;
56 // we could possibly simply typecast it to integer, but there are
57 // certain fringe cases that must not return an integer.
59 // clip leading sign
60 if ($this->negative && $integer[0] === '-') {
61 $digits = substr($integer, 1);
62 if ($digits === '0') {
63 $integer = '0';
64 } // rm minus sign for zero
65 } elseif ($this->positive && $integer[0] === '+') {
66 $digits = $integer = substr($integer, 1); // rm unnecessary plus
67 } else {
68 $digits = $integer;
71 // test if it's numeric
72 if (!ctype_digit($digits)) {
73 return false;
76 // perform scope tests
77 if (!$this->zero && $integer == 0) {
78 return false;
80 if (!$this->positive && $integer > 0) {
81 return false;
83 if (!$this->negative && $integer < 0) {
84 return false;
87 return $integer;
91 // vim: et sw=4 sts=4