Permalink
Browse files

Implemented prepending of query formatters, this way behaviors have

a bit more control over formatters they attach
  • Loading branch information...
1 parent 2e780bc commit da2f38f1605aab5b987d592d0910dcc6ef915dbc @lorenzo lorenzo committed Jan 23, 2014
Showing with 28 additions and 3 deletions.
  1. +24 −3 src/ORM/Query.php
  2. +4 −0 tests/TestCase/ORM/QueryTest.php
View
@@ -34,6 +34,21 @@
class Query extends DatabaseQuery {
/**
+ * Indicates that the operation should append to the list
+ */
+ const APPEND = 0;
+
+/**
+ * Indicates that the operation should prepend to the list
+ */
+ const PREPEND = 1;
+
+/**
+ * Indicates that the operation should overwrite the list
+ */
+ const OVERWRITE = true;
+
+/**
* Instance of a table object this query is bound to
*
* @var \Cake\ORM\Table
@@ -841,16 +856,22 @@ public function mapReduce(callable $mapper = null, callable $reducer = null, $ov
* }}}
*
* @param callable $formatter
- * @param boolean $overwrite
+ * @param boolean|integer $mode
* @return Cake\ORM\Query|array
*/
- public function formatResults(callable $formatter = null, $overwrite = false) {
- if ($overwrite) {
+ public function formatResults(callable $formatter = null, $mode = self::APPEND) {
+ if ($mode === self::OVERWRITE) {
$this->_formatters = [];
}
if ($formatter === null) {
return $this->_formatters;
}
+
+ if ($mode === self::PREPEND) {
+ array_unshift($this->_formatters, $formatter);
+ return $this;
+ }
+
$this->_formatters[] = $formatter;
return $this;
}
@@ -1769,6 +1769,10 @@ public function testFormatResults() {
$this->assertSame([$callback2], $query->formatResults());
$query->formatResults(null, true);
$this->assertSame([], $query->formatResults());
+
+ $query->formatResults($callback1);
+ $query->formatResults($callback2, $query::PREPEND);
+ $this->assertSame([$callback2, $callback1], $query->formatResults());
}
/**

0 comments on commit da2f38f

Please sign in to comment.