From 7bd568ce9ab0754df711f7b5b6fc3281e14cdfeb Mon Sep 17 00:00:00 2001 From: icewing Date: Thu, 3 Apr 2008 10:54:08 +0000 Subject: Marcus Povey * Added Iterator interface to override foreach behaviour git-svn-id: https://code.elgg.org/elgg/trunk@390 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'engine') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 60ad89733..ce5121d71 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -16,7 +16,10 @@ * This class holds methods for accessing the main entities table. * @author Marcus Povey */ - abstract class ElggEntity implements Exportable, Importable + abstract class ElggEntity implements + Exportable, // Allow export of data + Importable, // Allow import of data + Iterator // Override foreach behaviour { /** * The main attributes of an entity. @@ -299,6 +302,8 @@ return delete_entity($this->get('guid')); } + // EXPORTABLE INTERFACE //////////////////////////////////////////////////////////// + /** * Export this class into a stdClass containing all necessary fields. * Override if you wish to return more information than can be found in $this->attributes (shouldn't happen) @@ -315,6 +320,8 @@ return $tmp; } + // IMPORTABLE INTERFACE //////////////////////////////////////////////////////////// + /** * Import data from an parsed xml data array. * @@ -366,6 +373,40 @@ else throw new ImportException("Unsupported version ($version) passed to ElggEntity::import()"); } + + // ITERATOR INTERFACE ////////////////////////////////////////////////////////////// + /* + * This lets an entity's attributes be displayed using foreach as a normal array. + * Example: http://www.sitepoint.com/print/php5-standard-library + */ + + private $valid = FALSE; + + function rewind() + { + $this->valid = (FALSE !== reset($this->attributes)); + } + + function current() + { + return current($this->attributes); + } + + function key() + { + return key($this->attributes); + } + + function next() + { + $this->valid = (FALSE !== next($this->attributes)); + } + + function valid() + { + return $this->valid; + } + } /** -- cgit v1.2.3