<?php
/** JavaScript like Number class
* @author Andrea Giammarchi
* @license Mit Style
*/
class Number {
static protected $__CLASS__ = __CLASS__;
protected $__value__;
public function __construct($__value__ = 0){
switch(true){
case intval($__value__) === $__value__:
case floatval($__value__) === $__value__:
case $__value__ instanceof Number:
$this->__value__ = static::__value__($__value__);
break;
default:
$this->__value__ = (float)$__value__;
break;
}
}
public function __add($__value__){
return new static::$__CLASS__($this->__value__ + static::__value__($__value__));
}
public function __sub($__value__){
return new static::$__CLASS__($this->__value__ - static::__value__($__value__));
}
public function __mul($__value__){
return new static::$__CLASS__($this->__value__ * static::__value__($__value__));
}
public function __div($__value__){
return new static::$__CLASS__($this->__value__ / static::__value__($__value__));
}
public function __mod($__value__){
return new static::$__CLASS__($this->__value__ % static::__value__($__value__));
}
public function __sl($__value__){
return new static::$__CLASS__($this->__value__ << static::__value__($__value__));
}
public function __sr($__value__){
return new static::$__CLASS__($this->__value__ >> static::__value__($__value__));
}
public function __bw_or($__value__){
return new static::$__CLASS__($this->__value__ | static::__value__($__value__));
}
public function __bw_and($__value__){
return new static::$__CLASS__($this->__value__ & static::__value__($__value__));
}
public function __bw_xor($__value__){
return new static::$__CLASS__($this->__value__ ^ static::__value__($__value__));
}
public function __is_equal($__value__){
return $this->__value__ == static::__value__($__value__);
}
public function __is_not_equal($__value__){
return $this->__value__ != static::__value__($__value__);
}
public function __is_smaller($__value__){
return $this->__value__ < static::__value__($__value__);
}
public function __is_smaller_or_equal($__value__){
return $this->__value__ <= static::__value__($__value__);
}
public function __is_greater($__value__){
return $this->__value__ > static::__value__($__value__);
}
public function __is_greater_or_equal($__value__){
return $this->__value__ >= static::__value__($__value__);
}
public function __bw_not(){
return new static::$__CLASS__(~$this->__value__);
}
public function __bool(){
return $this->__value__ != 0;
}
public function __bool_not(){
return $this->__value__ == 0;
}
public function __assign_add($__value__){
$this->__value__ += static::__value__($__value__);
return $this;
}
public function __assign_sub($__value__){
$this->__value__ -= static::__value__($__value__);
return $this;
}
public function __assign_mul($__value__){
$this->__value__ *= static::__value__($__value__);
return $this;
}
public function __assign_div($__value__){
$this->__value__ /= static::__value__($__value__);
return $this;
}
public function __assign_mod($__value__){
$this->__value__ %= static::__value__($__value__);
return $this;
}
public function __assign_sl($__value__){
$this->__value__ <<= static::__value__($__value__);
return $this;
}
public function __assign_sr($__value__){
$this->__value__ >>= static::__value__($__value__);
return $this;
}
public function __assign_concat($__value__){
throw new Exception('unable to concatenate "'.$__value__.'" with a value of an instanceof '.static::$__CLASS__);
}
public function __assign_bw_or($__value__){
return new static::$__CLASS__($this->__value__ | static::__value__($__value__));
}
public function __assign_bw_and($__value__){
return new static::$__CLASS__($this->__value__ & static::__value__($__value__));
}
public function __assign_bw_xor($__value__){
return new static::$__CLASS__($this->__value__ ^ static::__value__($__value__));
}
public function __pre_inc(){
++$this->__value__;
return $this;
}
public function __pre_dec(){
--$this->__value__;
return $this;
}
public function __post_inc(){
return new static::$__CLASS__($this->__value__++);
}
public function __post_dec(){
return new static::$__CLASS__($this->__value__--);
}
public function __toString(){
return (string)$this->__value__;
}
static protected function __value__($__value__){
return $__value__ instanceof Number ? $__value__->__value__ : $__value__;
}
public function toExponential($decimal = 0){
return sprintf(func_num_args() ? '%.'.$decimal.'e' : '%e', $this->__value__);
}
public function toFixed($decimal = 0){
return (string)(0 < func_num_args() ?
round($this->__value__ * ($decimal = pow(10, $decimal))) / $decimal :
round($this->__value__)
);
}
public function toPrecision($decimal = 0){
if(0 < func_num_args()){
$length = strlen((int)$this->__value__);
if($decimal < $length)
$result = $this->toExponential($decimal);
elseif(strlen($this->__value__) <= $decimal)
$result = str_pad($this->__value__, $decimal, '0', STR_PAD_RIGHT);
else
$result = $this->toFixed($decimal - $length);
}
else
$result = (string)$this->__value__;
return $result;
return sprintf(func_num_args() ? '%.'.$decimal.'e' : '%e', $this->__value__);
}
public function toLocaleString(){
return is_int($this->__value__) ?
(string)$this->__value__ :
number_format($this->__value__, strlen($this->__value__) - strpos($this->__value__, '.') - 1)
;
}
public function toSource(){
return 'new Number('.$this->__value__.')';
}
public function toString(){
return (string)$this->__value__;
}
public function valueOf(){
return $this->__value__;
}
}
/**
* possible extensions
*/
class Int extends Number {
static protected $__CLASS__ = __CLASS__;
public function __construct($__value__ = 0){
$this->__value__ = static::__value__($__value__);
}
static protected function __value__($__value__){
return intval($__value__ instanceof Number ? $__value__->__value__ : $__value__);
}
}
class Float extends Number {
static protected $__CLASS__ = __CLASS__;
public function __construct($__value__ = 0){
$this->__value__ = static::__value__($__value__);
}
static protected function __value__($__value__){
return floatval($__value__ instanceof Number ? $__value__->__value__ : $__value__);
}
}
?>