<?php
/**
* Defines a class to be used for Integer Data Typing with PHP
*
* @author Pablo Santiago Sánchez <hide@address.com>
* @copyright Copyright (c) 2008, Pablo Santiago Sánchez
* @license http://opensource.org/licenses/bsd-license.php BSD License
* @package pop
* @subpackage datatypes
*/
/**
* Class used to handle integers.
* Only PHP_INT_MAX allowed.
* Integer max value is different from 32 to 64 bits processors
*
* @package pop
* @subpackage datatypes
*/
class PInteger extends PTypeBase
{
/**
* Constructor, in which you must inform the default value.
* If none is informed, it uses 0 as default
* @param string $defaultval Default value. Must be a number
*/
public function __construct($defaultval = 0)
{
$this->__set("value", $defaultval);
$this->defaultval = $defaultval;
}
/**
* Magic method used to set the value and check if it's valid
* @param string $name name of the value
* @param string $value value itself
* @return bool
*/
public function __set($name, $value)
{
if ($value === "")
$value = null;
if ($value > PHP_INT_MAX)
{
throw new Exception("This value is bigger than what PHP can handle for integers. Use Float instead.\nValue: <font color=#FF0000>".$value."</font>\nCurrent PHP_INT_MAX: <font color=#FF0000>".PHP_INT_MAX."</font>");
}
if ((!is_null($this->defaultval) && !is_null($value)) && !ctype_digit((string)$value) && $value != null)
{
throw new Exception("This value must be an integer or at least an string with an integer value.\nValue: <font color=#FF0000>".$value."</font>");
}
else if ($name != "value")
{
throw new Exception("This attribute cannot be set");
}
else
{
if(is_null($this->defaultval) && is_null($value))
$this->$name = null;
else
$this->$name = (int)$value;
return true;
}
}
}
?>