<?php
 
/**
 
 * @package DATA
 
 */
 
 
/**
 
 * ANSI SQL Character data type representation.
 
 * 
 
 * When inboxing, if the field cannot hold the desired value
 
 * {@link DATA_StringTooLarge} is thrown.
 
 * 
 
 * Field contents are padded to the field size with spaces.
 
 * 
 
 * @todo String operations.
 
 */
 
class DATA_SQLChar extends DATA_SQLType {
 
    /**
 
     * How many characters are stored by this field type.
 
     * 
 
     * @var int
 
     */
 
    protected $size;
 
    /**
 
     * The characters stored in this instance.
 
     * 
 
     * @var string
 
     */
 
    protected $chars;
 
    
 
    /**
 
     * Construct a sql char type with requested field size and initial value (optional).
 
     * 
 
     * Throws {@link DATA_StringTooLarge} when the chars to be stored are more than the field can hold.
 
     * 
 
     * @param boolean $nullable True if the type is nullable.
 
     * @param int $size The field size.
 
     * @param null|string $chars The characters stored in this object.
 
     */
 
    public function __construct($nullable, $size, $chars = '') {
 
        $this->size = $size;
 
        $this->setChars($chars);
 
        parent::__construct($nullable, is_null($chars));
 
    }
 
    
 
    /**
 
     * Returns the field size.
 
     * 
 
     * @return int Field size.
 
     */
 
    public function getSize() {
 
        return $this->size;
 
    }
 
    
 
    /**
 
     * Returns the characters stored in this field.
 
     * 
 
     * @return string The characters stored.
 
     */
 
    public function getChars() {
 
        return str_pad($this->chars, $this->size);
 
    }
 
    
 
    /**
 
     * Sets the chars stored in this field.
 
     * 
 
     * Throws {@link DATA_StringTooLarge} when the chars to be stored are more than the field can hold.
 
     * 
 
     * @param string $chars The characters stored in this object.
 
     */
 
    public function setChars($chars) {
 
        if (strlen($chars) > $this->size) {
 
            throw new DATA_StringTooLarge($this->size, $chars);
 
        }
 
        $this->chars = $chars;
 
        $this->setNotNull();
 
    }
 
    
 
    public function setNull() {
 
        parent::setNull();
 
        $this->chars = null;
 
    }
 
    
 
    public function __toString() {
 
        return $this->getChars();
 
    }
 
}
 
?>
 
 
 |