* @package Mammut\DB\Model */ class ColumnInfo extends \Mammut\StrictObject implements Types, XMLModel { /** * sql attribute: allow null in field */ const P_ALLOW_NULL = "null"; /** * sql attribute: automatic increasing number */ const P_AUTONUM = "autonum"; /** * sql attribute: unsigned number */ const P_UNSIGNED = "unsigned"; private $name; private $type; private $size = -1; private $default; private $param; private $charset; private $collate; /** * Creates a new column info object * * @param $name string * of the column * @param $type string * of one of the TYPE_* constants * @param $size int * (only valid for: char, vchar, binary) * @param $param array * parameters separated by a |, values(v1,v2) for enum, for other see the * P_* constants * @param $charset string * charset for character types, default is UTF8 * @param $collate string * collate, default is UTF8-BIN (if avaible) * @param $default mixed * value */ public function __construct($name, $type, $size = -1, array $param = array(), $charset = NULL, $collate = NULL, $default = NULL) { $this->name = $name; $this->type = $type; $this->size = $size; $this->default = $default; $this->param = $param; $this->charset = $charset ? $charset : Charset::CS_UTF8; $this->collate = $collate ? $collate : Charset::COL_BIN; } public function getName() { return (string) $this->name; } public function setName($name) { $this->name = (string) $name; } public function getType() { return (string) $this->type; } public function getSize() { return $this->size; } public function setSize($size) { $this->size = $size; } public function getDefault() { return $this->default; } public function getCharset() { return $this->charset; } public function getCollate() { return $this->collate; } public function isNullAllowed() { return in_array(self::P_ALLOW_NULL, $this->param); } public function isAutoNum() { return in_array(self::P_AUTONUM, $this->param); } public function isUnsigned() { return in_array(self::P_UNSIGNED, $this->param); } public function getParam() { return $this->param; } public static function fromXML($data) { $xml = simplexml_load_string($data); $name = (string) $xml['name']; $type = (string) $xml['type']; $size = isset($xml['size']) && ((string) $xml['size']) !== '' ? (int) $xml['size'] : -1; $default = isset($xml['default']) ? $xml['default'] : NULL; $param = isset($xml['extra']) ? explode('|', $xml['extra']) : array(); $charset = isset($xml['charset']) ? (string) $xml['charset'] : false; $collate = isset($xml['collate']) ? (string) $xml['collate'] : false; if(in_array($type, array( self::TYPE_BOOLEAN,self::TYPE_TEXT,self::TYPE_BLOB,self::TYPE_DATE, self::TYPE_DATETIME))) $size = -1; $result = new ColumnInfo($name, $type, $size, $param, $charset, $collate, $default); return $result; } public function toXML() { $extra = array(); if($this->isAutoNum()) $extra[] = self::P_AUTONUM; if($this->isNullAllowed()) $extra[] = self::P_ALLOW_NULL; $xml = 'name . '"'; $xml .= ' type="' . $this->type . '"'; if($this->type == self::TYPE_DECIMAL) $xml .= ' size="' . implode('|', $this->size) . '"'; elseif($this->size >= 0) $xml .= ' size="' . $this->size . '"'; if(!is_null($this->default)) $xml .= ' default="' . $this->default . '"'; if(count($extra) > 0) { $xml .= ' extra="' . implode('|', $extra) . '"'; } if(in_array($this->type, array(self::TYPE_CHAR,self::TYPE_VCHAR,self::TYPE_TEXT))) { $xml .= ' charset="' . $this->charset . '"'; $xml .= ' collate="' . $this->collate . '"'; } $xml .= '/>'; return $xml; } }