1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-02 12:21:06 +02:00

DibiResult: added setFormat()

This commit is contained in:
David Grudl
2012-01-18 21:08:34 +01:00
parent b964887f9d
commit ea0d6d02ba
3 changed files with 37 additions and 14 deletions

View File

@@ -480,7 +480,8 @@ class DibiConnection extends DibiObject
*/ */
public function createResultSet(IDibiResultDriver $resultDriver) public function createResultSet(IDibiResultDriver $resultDriver)
{ {
return new DibiResult($resultDriver, $this->config['result']); $res = new DibiResult($resultDriver);
return $res->setFormat(dibi::DATETIME, $this->config['result']['formatDateTime']);
} }

View File

@@ -54,23 +54,18 @@ class DibiResult extends DibiObject implements IDataSource
/** @var string returned object class */ /** @var string returned object class */
private $rowClass = 'DibiRow'; private $rowClass = 'DibiRow';
/** @var string date-time format */ /** @var array format */
private $dateFormat = ''; private $formats = array();
/** /**
* @param IDibiResultDriver * @param IDibiResultDriver
* @param array
*/ */
public function __construct($driver, $config) public function __construct($driver)
{ {
$this->driver = $driver; $this->driver = $driver;
$this->detectTypes(); $this->detectTypes();
if (!empty($config['formatDateTime'])) {
$this->dateFormat = is_string($config['formatDateTime']) ? $config['formatDateTime'] : '';
}
} }
@@ -525,18 +520,18 @@ class DibiResult extends DibiObject implements IDataSource
} elseif ($type === dibi::DATE || $type === dibi::DATETIME) { } elseif ($type === dibi::DATE || $type === dibi::DATETIME) {
if ((int) $value === 0) { // '', NULL, FALSE, '0000-00-00', ... if ((int) $value === 0) { // '', NULL, FALSE, '0000-00-00', ...
} elseif ($this->dateFormat === '') { // return DateTime object (default) } elseif (empty($this->formats[$type])) { // return DateTime object (default)
$row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value); $row[$key] = new DibiDateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
} elseif ($this->dateFormat === 'U') { // return timestamp } elseif ($this->formats[$type] === 'U') { // return timestamp
$row[$key] = is_numeric($value) ? (int) $value : strtotime($value); $row[$key] = is_numeric($value) ? (int) $value : strtotime($value);
} elseif (is_numeric($value)) { // formatted date } elseif (is_numeric($value)) { // formatted date
$row[$key] = date($this->dateFormat, $value); $row[$key] = date($this->formats[$type], $value);
} else { } else {
$value = new DibiDateTime($value); $value = new DibiDateTime($value);
$row[$key] = $value->format($this->dateFormat); $row[$key] = $value->format($this->formats[$type]);
} }
} elseif ($type === dibi::BINARY) { } elseif ($type === dibi::BINARY) {
@@ -572,6 +567,31 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Sets data format.
* @param string type (use constant Dibi::*)
* @param string format
* @return DibiResult provides a fluent interface
*/
final public function setFormat($type, $format)
{
$this->formats[$type] = $format;
return $this;
}
/**
* Returns data format.
* @return string
*/
final public function getFormat($type)
{
return isset($this->formats[$type]) ? $this->formats[$type] : NULL;
}
/********************* meta info ****************d*g**/ /********************* meta info ****************d*g**/

View File

@@ -20,7 +20,9 @@ dibi::connect(array(
$res = dibi::query('SELECT * FROM [customers]'); $res = dibi::query('SELECT * FROM [customers]');
$res->setType('customer_id', Dibi::INTEGER) $res->setType('customer_id', Dibi::INTEGER)
->setType('added', Dibi::DATETIME); ->setType('added', Dibi::DATETIME)
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s');
Debugger::dump( $res->fetch() ); Debugger::dump( $res->fetch() );
// outputs: // outputs: