- interface iconvertible
- interface ivalued
- class psqlx_boolean
- global function box
Code: Select all
<?php
//Interface allowing an object to be converted to boxed and unboxed versions of himself
interface psqlx_iconvertible{
//Non boxed conversions
public function unbox();
//Boxed conversions
public function toint8($signed = true, $lowbound = NULL, $highbound = NULL);
public function toint16($signed = true, $lowbound = NULL, $highbound = NULL);
public function toint24($signed = true, $lowbound = NULL, $highbound = NULL);
public function toint32($signed = true, $lowbound = NULL, $highbound = NULL);
public function toint64($signed = true, $lowbound = NULL, $highbound = NULL);
public function tosingle($decimals = NULL, $lowbound = NULL, $highbound = NULL);
public function todouble($decimals = NULL, $lowbound = NULL, $highbound = NULL);
public function tostring($lowbound = NULL, $highbound = NULL, $regexp = NULL);
public function todatetime($lowbound = NULL, $highbound = NULL);
public function toboolean();
public function tointerval();
public function toarray($strict = true);
}
//Interface allowing an object to represent a value
interface psqlx_ivalued{
public function setValue($value);
public function getValue();
}
//Class representing a boolean value
if(!class_exists('psqlx_boolean')){
class psqlx_boolean implements psqlx_iconvertible {
private $d = false;
public function __construct($value){ $this->setValue($value); }
//psqlx_iconvertible
public function unbox(){ return $this->d; }
public function toint8($signed = true, $lowbound = NULL, $highbound = NULL){ }
public function toint16($signed = true, $lowbound = NULL, $highbound = NULL){ }
public function toint24($signed = true, $lowbound = NULL, $highbound = NULL){ }
public function toint32($signed = true, $lowbound = NULL, $highbound = NULL){ }
public function toint64($signed = true, $lowbound = NULL, $highbound = NULL){ }
public function tosingle($decimals = NULL, $lowbound = NULL, $highbound = NULL){ }
public function todouble($decimals = NULL, $lowbound = NULL, $highbound = NULL){ }
public function tostring($lowbound = NULL, $highbound = NULL, $regexp = NULL){ }
public function todatetime($lowbound = NULL, $highbound = NULL){ }
public function toboolean(){ }
public function tointerval(){ }
public function toarray($strict = true){ }
//psqlx_ivalued
public function setValue($value){
if($value !== true && $value !== false){ throw new Exception($value.' is not of boolean type, cannot store this value into a psqlx_boolean.'); }
$this->d = $value;
}
public function getValue(){ return $this->d; }
//Quick access to set/getvalue through magic methods
public function __set($name, $value){
switch($name){
case 'v':
case 'val':
case 'value':
return $this->setValue($value);
default: throw new Exception('Unknown '.$name.' in psqlx_boolean');
}
}
public function __get($name){
switch($name){
case 'v':
case 'val':
case 'value':
return $this->getValue();
default: throw new Exception('Unknown '.$name.' in psqlx_boolean');
}
}
}
}
//This function boxes values into objects for psqlx2.0
function box($value){
if(is_null($value)){ return $value; }
if(is_bool($value)){ return new psqlx_boolean($value); }
if(is_object($value)){ return $value; }
if(is_resource($value)){ return $value; }
if(is_string($value)){ return ''; }
throw new Exception('Unable to box scalar $value ('.$value.') to a boxed psqlx type');
}
?>
Code: Select all
<pre>
<?php
include('psqlx.types.php');
$d = box(true);
$d->v = false;
$d->val = false;
$d->value = false;
$d->v = true;
$d->val = true;
$d->value = true;
$d->v = 1;
$d->val = 2;
$d->value = 3;
?>
</pre>