Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Starting to implemente lazy result formatters

  • Loading branch information...
commit b2e093aa0856b1726a6391d83582e0de42bdc2e3 1 parent 4c24185
@lorenzo lorenzo authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 src/ORM/Query.php
View
28 src/ORM/Query.php
@@ -114,6 +114,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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.