mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
Merge branch 'MDL-53430-master' of git://github.com/merrill-oakland/moodle
This commit is contained in:
commit
1e60d7e482
2 changed files with 23 additions and 10 deletions
|
@ -57,7 +57,7 @@ class recordset_walk implements \Iterator {
|
||||||
protected $callback;
|
protected $callback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array|false Extra params for the callback.
|
* @var mixed|null Extra param for the callback.
|
||||||
*/
|
*/
|
||||||
protected $callbackextra;
|
protected $callbackextra;
|
||||||
|
|
||||||
|
@ -66,9 +66,9 @@ class recordset_walk implements \Iterator {
|
||||||
*
|
*
|
||||||
* @param \moodle_recordset $recordset Recordset to iterate.
|
* @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 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->recordset = $recordset;
|
||||||
$this->callback = $callback;
|
$this->callback = $callback;
|
||||||
$this->callbackextra = $callbackextra;
|
$this->callbackextra = $callbackextra;
|
||||||
|
@ -99,10 +99,10 @@ class recordset_walk implements \Iterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply callback and return.
|
// Apply callback and return.
|
||||||
if ($this->callbackextra) {
|
if (!is_null($this->callbackextra)) {
|
||||||
return call_user_func($this->callback, $record);
|
|
||||||
} else {
|
|
||||||
return call_user_func($this->callback, $record, $this->callbackextra);
|
return call_user_func($this->callback, $record, $this->callbackextra);
|
||||||
|
} else {
|
||||||
|
return call_user_func($this->callback, $record);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,11 +45,14 @@ class core_recordset_walk_testcase extends advanced_testcase {
|
||||||
|
|
||||||
$recordset = $DB->get_recordset('assign');
|
$recordset = $DB->get_recordset('assign');
|
||||||
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
||||||
$this->assertEquals(0, iterator_count($walker));
|
|
||||||
$this->assertFalse($walker->valid());
|
$this->assertFalse($walker->valid());
|
||||||
|
|
||||||
|
$count = 0;
|
||||||
foreach ($walker as $data) {
|
foreach ($walker as $data) {
|
||||||
// No error here.
|
// No error here.
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
|
$this->assertEquals(0, $count);
|
||||||
$walker->close();
|
$walker->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,11 +68,14 @@ class core_recordset_walk_testcase extends advanced_testcase {
|
||||||
// Simple iteration.
|
// Simple iteration.
|
||||||
$recordset = $DB->get_recordset('assign');
|
$recordset = $DB->get_recordset('assign');
|
||||||
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
||||||
$this->assertEquals(10, iterator_count($walker));
|
|
||||||
|
$count = 0;
|
||||||
foreach ($walker as $data) {
|
foreach ($walker as $data) {
|
||||||
// Checking that the callback is being executed on each iteration.
|
// Checking that the callback is being executed on each iteration.
|
||||||
$this->assertEquals($data->id . ' potatoes', $data->newfield);
|
$this->assertEquals($data->id . ' potatoes', $data->newfield);
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
|
$this->assertEquals(10, $count);
|
||||||
// No exception if we double-close.
|
// No exception if we double-close.
|
||||||
$walker->close();
|
$walker->close();
|
||||||
}
|
}
|
||||||
|
@ -85,17 +91,22 @@ class core_recordset_walk_testcase extends advanced_testcase {
|
||||||
|
|
||||||
// Iteration with extra callback arguments.
|
// Iteration with extra callback arguments.
|
||||||
$recordset = $DB->get_recordset('assign');
|
$recordset = $DB->get_recordset('assign');
|
||||||
|
|
||||||
$walker = new \core\dml\recordset_walk(
|
$walker = new \core\dml\recordset_walk(
|
||||||
$recordset,
|
$recordset,
|
||||||
array($this, 'extra_callback'),
|
array($this, 'extra_callback'),
|
||||||
array('brown' => 'onions')
|
array('brown' => 'onions')
|
||||||
);
|
);
|
||||||
$this->assertEquals(10, iterator_count($walker));
|
|
||||||
|
$count = 0;
|
||||||
foreach ($walker as $data) {
|
foreach ($walker as $data) {
|
||||||
// Checking that the callback is being executed on each
|
// Checking that the callback is being executed on each
|
||||||
// iteration and the param is being passed.
|
// iteration and the param is being passed.
|
||||||
$this->assertEquals('onions', $data->brown);
|
$this->assertEquals('onions', $data->brown);
|
||||||
|
$count++;
|
||||||
}
|
}
|
||||||
|
$this->assertEquals(10, $count);
|
||||||
|
|
||||||
$walker->close();
|
$walker->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +116,9 @@ class core_recordset_walk_testcase extends advanced_testcase {
|
||||||
* @param stdClass $data
|
* @param stdClass $data
|
||||||
* @return \Traversable
|
* @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';
|
$data->newfield = $data->id . ' potatoes';
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue