Skip to content

Commit

Permalink
MDL-39846 add event property iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 19, 2013
1 parent 27af3e6 commit ed17808
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/classes/event/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @property-read mixed $other array or scalar, can not contain objects
* @property-read int $timecreated
*/
abstract class base {
abstract class base implements \IteratorAggregate {
/** @var array event data */
protected $data;

Expand Down Expand Up @@ -183,7 +183,7 @@ public static final function create(array $data = null) {
$event->init();

// Warn developers if they do something wrong.
if (debugging('', DEBUG_DEVELOPER)) {
if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced.
static $automatickeys = array('eventname', 'component', 'action', 'object', 'timecreated');
static $initkeys = array('crud', 'level', 'objecttable');

Expand Down Expand Up @@ -514,7 +514,7 @@ public function add_record_snapshot($tablename, $record) {

// NOTE: this might use some kind of MUC cache,
// hopefully we will not run out of memory here...
if (debugging('', DEBUG_DEVELOPER)) {
if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced.
if (!$DB->get_manager()->table_exists($tablename)) {
debugging("Invalid table name '$tablename' specified, database table does not exist.");
}
Expand Down Expand Up @@ -580,4 +580,13 @@ public function __set($name, $value) {
public function __isset($name) {
return isset($this->data[$name]);
}

/**
* Create an iterator because magic vars can't be seen by 'foreach'.
*
* @return \ArrayIterator
*/
public function getIterator() {
return new \ArrayIterator($this->data);
}
}
11 changes: 11 additions & 0 deletions lib/tests/event_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,15 @@ public function test_record_snapshots() {
$this->assertEquals(1, $user->id);
$this->assertSame('guest', $user->username);
}

public function test_iteration() {
$event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));

$data = array();
foreach ($event as $k => $v) {
$data[$k] = $v;
}

$this->assertSame($event->get_data(), $data);
}
}

0 comments on commit ed17808

Please sign in to comment.