Skip to content
Permalink
Browse files

Starting to implemente lazy result formatters

  • Loading branch information...
lorenzo committed Jan 1, 2014
1 parent 4c24185 commit b2e093aa0856b1726a6391d83582e0de42bdc2e3
Showing with 28 additions and 0 deletions.
  1. +28 −0 src/ORM/Query.php
@@ -113,6 +113,14 @@ class Query extends DatabaseQuery {
*/
protected $_mapReduce = [];
/**
* List of formatter classes or callbacks that will post-process the
* results when fetched
*
* @var array
*/
protected $_formatters = [];
/**
* Holds any custom options passed using applyOptions that could not be processed
* by any method in this class.
@@ -787,6 +795,17 @@ public function mapReduce(callable $mapper = null, callable $reducer = null, $ov
return $this;
}
public function formatResults(callable $formatter = null, $overwrite = false) {
if ($overwrite) {
$this->_formatters = [];
}
if ($formatter === null) {
return $this->_formatters;
}
$this->_formatters[] = $formatter;
return $this;
}
/**
* Returns the first result out of executing this query, if the query has not been
* executed before, it will set the limit clause to 1 for performance reasons.
@@ -860,6 +879,15 @@ protected function _decorateResults($result) {
if (!empty($this->_mapReduce)) {
$result = new ResultSetDecorator($result);
}
foreach ($this->_formatters as $formatter) {
$result = $formatter($this, $result);
}
if (!empty($this->_formatters) && !($result instanceof ResultSetDecorator)) {
$result = new ResultSetDecorator($result);
}
return $result;
}

0 comments on commit b2e093a

Please sign in to comment.
You can’t perform that action at this time.