Skip to content

Commit

Permalink
Call callback with passing result
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Vrana committed Jan 18, 2012
1 parent 1b71499 commit da3ebdf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions NotORM.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,5 +91,17 @@ function __call($table, array $where) {
} }
return $return; return $return;
} }

/** Pass results to callback
* @param NotORM_Result|NotORM_Row
* @param ...
* @param callback it will get results in arguments
* @return null
*/
static function then($result1, $callback) {
$results = func_get_args();
$callback = array_pop($results);
call_user_func_array($callback, $results); // don't return its result to be forward compatible with deferred calls
}


} }
9 changes: 9 additions & 0 deletions NotORM/Result.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -630,6 +630,15 @@ function fetchPairs($key, $value = '') {
return $return; return $return;
} }


/** Pass result to callback
* @param callback it will get $this in argument
* @return null
*/
function then($callback) {
return NotORM::then($this, $callback);
// don't return $this - should be at the end of fluent call
}

protected function access($key, $delete = false) { protected function access($key, $delete = false) {
if ($delete) { if ($delete) {
if (is_array($this->access)) { if (is_array($this->access)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/32-then.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
include_once dirname(__FILE__) . "/connect.inc.php";

$software->author()->order("id")->then(function ($authors) {
foreach ($authors as $author) {
echo "$author[name]\n";
}
});
echo "\n";

$software->application_tag()->order("application_id, tag_id")->then(function ($application_tags) {
foreach ($application_tags as $application_tag) {
NotORM::then($application_tag->application, $application_tag->tag, function ($application, $tag) {
echo "$application[title]: $tag[name]\n";
});
}
});
20 changes: 20 additions & 0 deletions tests/32-then.phpt
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Passing result to callback
--SKIPIF--
<?php
echo (version_compare(PHP_VERSION, '5.3.0') < 0 ? "PHP 5.3+ required\n" : "");
?>
--FILE--
<?php
include dirname(__FILE__) . "/32-then.php";
?>
--EXPECTF--
Jakub Vrana
David Grudl

Adminer: PHP
Adminer: MySQL
JUSH: JavaScript
Nette: PHP
Dibi: PHP
Dibi: MySQL

0 comments on commit da3ebdf

Please sign in to comment.