aboutsummaryrefslogtreecommitdiff
path: root/engine/classes
diff options
context:
space:
mode:
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-13 04:56:23 +0000
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-02-13 04:56:23 +0000
commitffee3ca681dc177415f6fe91b05539df7c865228 (patch)
tree9024174bfcbb36683b7fca6eca3b5b033058bca0 /engine/classes
parent767a9310e2bd88e26ab3ab92b7cb6a52a935787b (diff)
downloadelgg-ffee3ca681dc177415f6fe91b05539df7c865228.tar.gz
elgg-ffee3ca681dc177415f6fe91b05539df7c865228.tar.bz2
Added ability to get results of callback function in ElggBatch.
git-svn-id: http://code.elgg.org/elgg/trunk@8197 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/classes')
-rw-r--r--engine/classes/ElggBatch.php47
1 files changed, 42 insertions, 5 deletions
diff --git a/engine/classes/ElggBatch.php b/engine/classes/ElggBatch.php
index a69fb9dc9..0429e94ff 100644
--- a/engine/classes/ElggBatch.php
+++ b/engine/classes/ElggBatch.php
@@ -11,6 +11,19 @@
* can be a string, an array, or a closure.
* {@link http://php.net/manual/en/language.pseudo-types.php}
*
+ * Results from the callback are stored in callbackResult.
+ * If the callback returns only booleans callbackResults will be the combined
+ * result of all calls.
+ *
+ * If the callback returns anything else callbackresult will be an indexed array
+ * of whatever the callback returns. If returning error handling information,
+ * you should include enough information to determine which result you're referring
+ * to.
+ *
+ * Don't combine returning bools and returning something else.
+ *
+ * Note that returning false will not stop the foreach.
+ *
* @example
* <code>
* $batch = new ElggBatch('elgg_get_entities', array());
@@ -21,6 +34,7 @@
*
* $callback = function($result, $getter, $options) {
* var_dump("Going to delete annotation id: $result->id");
+ * return true;
* }
*
* $batch = new ElggBatch('elgg_get_annotations', array('guid' => 2), $callback);
@@ -102,11 +116,11 @@ class ElggBatch
private $chunkIndex = 0;
/**
+ * The number of results iterated through
*
- *
- * @var unknown_type
+ * @var int
*/
- public $processedResults = 0;
+ private $processedResults = 0;
/**
* Is the getter a valid callback
@@ -116,6 +130,13 @@ class ElggBatch
private $validGetter = null;
/**
+ * The result of running all entities through the callback function.
+ *
+ * @var mixed
+ */
+ public $callbackResult = null;
+
+ /**
* Batches operations on any elgg_get_*() functions that supports
* an options array.
*
@@ -153,11 +174,27 @@ class ElggBatch
foreach ($batch as $result) {
if (is_string($callback)) {
- $callback($result, $getter, $options);
+ $result = $callback($result, $getter, $options);
+ } else {
+ $result = call_user_func_array($callback, array($result, $getter, $options));
+ }
+
+ if (!isset($all_results)) {
+ if ($result === true || $result === false || $result === null) {
+ $all_results = $result;
+ } else {
+ $all_results = array();
+ }
+ }
+
+ if (($result === true || $result === false || $result === null) && !is_array($all_results)) {
+ $all_results = $result && $all_results;
} else {
- call_user_func_array($callback, array($result, $getter, $options));
+ $all_results[] = $result;
}
}
+
+ $this->callbackResult = $all_results;
}
}