<?php
/**
* @package DATA
*/
/**
* ANSI SQL small integer data type representation.
*
* When inboxing, if the field cannot hold the desired value
* {@link DATA_InvalidInt} is thrown.
*/
class DATA_SQLSmallInt extends DATA_SQLType implements DATA_Number {
/**
* The stored number.
* @var int
*/
protected $number;
/**
* Construct a sql integer type with an initial value (optional).
*
* Throws {@link DATA_InvalidInt} when the number to be stored is invalid.
*
* @param boolean $nullable True if the type is nullable.
* @param null|int|string $number The stored number.
*/
public function __construct($nullable, $number = 0) {
$this->setNumber($number);
parent::__construct($nullable, is_null($number));
}
/**
* Returns the stored number.
*
* @return int The stored number.
*/
public function getNumber() {
return $this->number;
}
/**
* Sets the stored number.
*
* Throws {@link DATA_InvalidInt} when the number to be stored is invalid.
*
* @param int|string $number The stored number.
*/
public function setNumber($number) {
$toInt = (int)$number;
if (is_string($number)) {
if ((string)$toInt !== $number) {
throw new DATA_InvalidInt($number);
}
}
if ($toInt > 32767 || $toInt < -32768) {
throw new DATA_InvalidInt($number);
}
$this->number = (int)$number;
$this->setNotNull();
}
public function setNull() {
parent::setNull();
$this->number = null;
}
public function __toString() {
return (string)$this->number;
}
public function outbox() {
return $this->number;
}
/**
* Adds this int to another and returns the result.
*
* Throws {@link DATA_InvalidInt}.
*
* @param DATA_Number $other The number to add.
* @return DATA_SQLSmallInt The result.
*/
public function add(DATA_Number $other) {
return new DATA_SQLSmallInt($this->isNullable(), $this->number + $other->getNumber());
}
/**
* Substracts this int to another and returns the result.
*
* Throws {@link DATA_InvalidInt}.
*
* @param DATA_Number $other The number to substract.
* @return DATA_SQLSmallInt The result.
*/
public function substract(DATA_Number $other) {
return new DATA_SQLSmallInt($this->isNullable(), $this->number - $other->getNumber());
}
/**
* Multiplies this int to another and returns the result.
*
* Throws {@link DATA_InvalidInt}.
*
* @param DATA_Number $other The number to multiply by.
* @return DATA_SQLSmallInt The result.
*/
public function multiply(DATA_Number $other) {
return new DATA_SQLSmallInt($this->isNullable(), $this->number * $other->getNumber());
}
/**
* Divides this int to another and returns the result.
*
* Throws {@link DATA_InvalidInt}.
*
* @param DATA_Number $other The number to divide by.
* @return DATA_SQLSmallInt The result.
*/
public function divide(DATA_Number $other) {
return new DATA_SQLSmallInt($this->isNullable(), (int)($this->number / $other->getNumber()));
}
}
?>
|