<?php
/**
* @package DATA
*/
/**
* A concrete factory for inboxing string with mysql date format into date objects.
*/
class DATA_MySQL5_SQLDateFactory extends DATA_SQLTypeFactory {
/**
* Flags the type to nullable or not nullable.
* @var boolean
*/
private $nullable;
/**
* Constructor.
*
* @param boolean $nullable True if the type is nullable.
*/
public function __construct($nullable) {
$this->nullable = $nullable;
}
/**
* Inboxes a value.
*
* Throws {@link DATA_InvalidDate}.
*
* @param mixed $value The value.
* @return DATA_SQLDate Inboxed value.
*/
public function inbox($value) {
if ($value instanceof DATA_SQLDate) {
if ($this->nullable == $value->isNullable()) {
return clone $value;
}
}
if ($value instanceof DATA_SQLType) {
$value = $value->outbox();
}
if ($value === null) {
return new DATA_SQLDate($this->nullable);
}
if (!preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/', $value)) {
throw new DATA_InvalidDate($value);
}
list($year, $month, $day) = explode('-', $value);
return new DATA_SQLDate($this->nullable, $year, $month, $day);
}
}
?>
|