PHP Classes

File: DATA/SQLType.php

Recommend this page to a friend!
  Classes of Martin Alterisio   DATA   DATA/SQLType.php   Download  
File: DATA/SQLType.php
Role: Class source
Content type: text/plain
Description: SQL type representation.
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change: + anonymous access
Date: 17 years ago
Size: 2,820 bytes
 

Contents

Class file image Download
<?php
/**
 * @package DATA
 */

/**
 * SQL type representation.
 *
 * Right now there will be no support for arbitrary size math
 * operations, but these will be supported by the member
 * functions in the corresponding classes. Outboxing into native
 * php types and doing the math operations outside is discouraged.
 *
 * All classes will provide __toString() functionality for automatic
 * outboxing into string representation. This will allow character
 * types to be used in the usual string context without having to
 * explicitly outbox the value.
 *
 */
abstract class DATA_SQLType {
   
/**
     * Flags the type to nullable or not nullable.
     * @var boolean
     */
   
private $nullable;
   
   
/**
     * Flags the value as null.
     * @var boolean
     */
   
private $isnull;
   
   
/**
     * Internal constructor. Sets the type to nullable or not nullable.
     *
     * @param boolean $nullable True if the type is nullable.
     * @param boolean $isnull True if the initial value is null.
     */
   
protected function __construct($nullable, $isnull) {
       
$this->nullable = $nullable;
       
$this->isnull = false;
        if (
$isnull) $this->setNull();
    }
   
   
/**
     * Automatically outboxes the value into a native php string.
     *
     * @return string String representation of the value.
     */
   
abstract public function __toString();
   
   
/**
     * Indicates if this type is nullable.
     *
     * @return boolean True if the type is nullable.
     */
   
public function isNullable() {
        return
$this->nullable;
    }
   
   
/**
     * Indicates if this value is null.
     *
     * Optionally, can be used statically passing the value by parameter.
     *
     * @return boolean True if the value is null.
     */
   
public function isNull() {
        if (isset(
$this)) {
            return
$this->isnull;
        }
       
$value = func_get_arg(0);
        if (
$value instanceof DATA_SQLType) {
            return
$value->isNull();
        } else {
            return
is_null($value);
        }
    }
   
   
/**
     * Nulls this value.
     *
     * Throws {@link DATA_NotNullable}.
     */
   
public function setNull() {
        if (!
$this->nullable) {
            throw new
DATA_NotNullable();
        }
       
$this->isnull = true;
    }
   
   
/**
     * Internally flag the value as not null.
     */
   
protected function setNotNull() {
       
$this->isnull = false;
    }
   
   
/**
     * Outboxes the value into a php native value.
     *
     * @return mixed Php native value.
     */
   
public function outbox() {
        if (
$this->isnull) return null;
        else return
$this->__toString();
    }
}
?>