friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/AttrDef/Integer.php

92 lines
2.5 KiB
PHP
Raw Normal View History

2010-09-08 23:14:17 -04:00
<?php
/**
* Validates an integer.
* @note While this class was modeled off the CSS definition, no currently
* allowed CSS uses this type. The properties that do are: widows,
* orphans, z-index, counter-increment, counter-reset. Some of the
* HTML attributes, however, find use for a non-negative version of this.
*/
class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
{
/**
2016-02-09 05:06:17 -05:00
* Whether or not negative values are allowed.
* @type bool
2010-09-08 23:14:17 -04:00
*/
protected $negative = true;
/**
2016-02-09 05:06:17 -05:00
* Whether or not zero is allowed.
* @type bool
2010-09-08 23:14:17 -04:00
*/
protected $zero = true;
/**
2016-02-09 05:06:17 -05:00
* Whether or not positive values are allowed.
* @type bool
2010-09-08 23:14:17 -04:00
*/
protected $positive = true;
/**
* @param $negative Bool indicating whether or not negative values are allowed
* @param $zero Bool indicating whether or not zero is allowed
* @param $positive Bool indicating whether or not positive values are allowed
*/
2016-02-09 05:06:17 -05:00
public function __construct($negative = true, $zero = true, $positive = true)
{
2010-09-08 23:14:17 -04:00
$this->negative = $negative;
2016-02-09 05:06:17 -05:00
$this->zero = $zero;
2010-09-08 23:14:17 -04:00
$this->positive = $positive;
}
2016-02-09 05:06:17 -05:00
/**
* @param string $integer
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
public function validate($integer, $config, $context)
{
2010-09-08 23:14:17 -04:00
$integer = $this->parseCDATA($integer);
2016-02-09 05:06:17 -05:00
if ($integer === '') {
return false;
}
2010-09-08 23:14:17 -04:00
// we could possibly simply typecast it to integer, but there are
// certain fringe cases that must not return an integer.
// clip leading sign
2016-02-09 05:06:17 -05:00
if ($this->negative && $integer[0] === '-') {
2010-09-08 23:14:17 -04:00
$digits = substr($integer, 1);
2016-02-09 05:06:17 -05:00
if ($digits === '0') {
$integer = '0';
} // rm minus sign for zero
} elseif ($this->positive && $integer[0] === '+') {
2010-09-08 23:14:17 -04:00
$digits = $integer = substr($integer, 1); // rm unnecessary plus
} else {
$digits = $integer;
}
// test if it's numeric
2016-02-09 05:06:17 -05:00
if (!ctype_digit($digits)) {
return false;
}
2010-09-08 23:14:17 -04:00
// perform scope tests
2016-02-09 05:06:17 -05:00
if (!$this->zero && $integer == 0) {
return false;
}
if (!$this->positive && $integer > 0) {
return false;
}
if (!$this->negative && $integer < 0) {
return false;
}
2010-09-08 23:14:17 -04:00
return $integer;
}
}
// vim: et sw=4 sts=4