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

DibiResult: added setRowFactory() [Closes #26]

This commit is contained in:
Teyras
2010-12-03 13:20:23 -08:00
committed by David Grudl
parent 8a07e1fbe4
commit 388067cb5d

View File

@@ -54,6 +54,9 @@ class DibiResult extends DibiObject implements IDataSource
/** @var string returned object class */ /** @var string returned object class */
private $rowClass = 'DibiRow'; private $rowClass = 'DibiRow';
/** @var Callback returned object factory*/
private $rowFactory;
/** @var array format */ /** @var array format */
private $formats = array(); private $formats = array();
@@ -203,6 +206,19 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Set a factory to create fetched object instances. These should extend the DibiRow class.
* @param callback
* @return DibiResult provides a fluent interface
*/
public function setRowFactory($callback)
{
$this->rowFactory = $callback;
return $this;
}
/** /**
* Fetches the row at current position, process optional type conversion. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
@@ -216,7 +232,9 @@ class DibiResult extends DibiObject implements IDataSource
} }
$this->fetched = TRUE; $this->fetched = TRUE;
$this->normalize($row); $this->normalize($row);
if ($this->rowClass) { if ($this->rowFactory) {
return call_user_func($this->rowFactory, $row);
} elseif ($this->rowClass) {
$row = new $this->rowClass($row); $row = new $this->rowClass($row);
} }
return $row; return $row;