Merge branch 'MDL-53430-master' of git://github.com/merrill-oakland/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2016-03-16 00:03:31 +01:00
commit 1e60d7e482
2 changed files with 23 additions and 10 deletions

View file

@ -57,7 +57,7 @@ class recordset_walk implements \Iterator {
protected $callback;
/**
* @var array|false Extra params for the callback.
* @var mixed|null Extra param for the callback.
*/
protected $callbackextra;
@ -66,9 +66,9 @@ class recordset_walk implements \Iterator {
*
* @param \moodle_recordset $recordset Recordset to iterate.
* @param callable $callback Apply this function to each record. If using a method, it should be public.
* @param array $callbackextra Array of arguments to pass to the callback.
* @param mixed $callbackextra An extra single parameter to pass to the callback. Use a container to pass multiple values.
*/
public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = false) {
public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = null) {
$this->recordset = $recordset;
$this->callback = $callback;
$this->callbackextra = $callbackextra;
@ -99,10 +99,10 @@ class recordset_walk implements \Iterator {
}
// Apply callback and return.
if ($this->callbackextra) {
return call_user_func($this->callback, $record);
} else {
if (!is_null($this->callbackextra)) {
return call_user_func($this->callback, $record, $this->callbackextra);
} else {
return call_user_func($this->callback, $record);
}
}

View file

@ -45,11 +45,14 @@ class core_recordset_walk_testcase extends advanced_testcase {
$recordset = $DB->get_recordset('assign');
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
$this->assertEquals(0, iterator_count($walker));
$this->assertFalse($walker->valid());
$count = 0;
foreach ($walker as $data) {
// No error here.
$count++;
}
$this->assertEquals(0, $count);
$walker->close();
}
@ -65,11 +68,14 @@ class core_recordset_walk_testcase extends advanced_testcase {
// Simple iteration.
$recordset = $DB->get_recordset('assign');
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
$this->assertEquals(10, iterator_count($walker));
$count = 0;
foreach ($walker as $data) {
// Checking that the callback is being executed on each iteration.
$this->assertEquals($data->id . ' potatoes', $data->newfield);
$count++;
}
$this->assertEquals(10, $count);
// No exception if we double-close.
$walker->close();
}
@ -85,17 +91,22 @@ class core_recordset_walk_testcase extends advanced_testcase {
// Iteration with extra callback arguments.
$recordset = $DB->get_recordset('assign');
$walker = new \core\dml\recordset_walk(
$recordset,
array($this, 'extra_callback'),
array('brown' => 'onions')
);
$this->assertEquals(10, iterator_count($walker));
$count = 0;
foreach ($walker as $data) {
// Checking that the callback is being executed on each
// iteration and the param is being passed.
$this->assertEquals('onions', $data->brown);
$count++;
}
$this->assertEquals(10, $count);
$walker->close();
}
@ -105,7 +116,9 @@ class core_recordset_walk_testcase extends advanced_testcase {
* @param stdClass $data
* @return \Traversable
*/
public function simple_callback($data) {
public function simple_callback($data, $nothing = 'notpassed') {
// Confirm nothing was passed.
$this->assertEquals('notpassed', $nothing);
$data->newfield = $data->id . ' potatoes';
return $data;
}