aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoricewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-04-25 16:07:52 +0000
committericewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-04-25 16:07:52 +0000
commit80462b4952c73923a369dfdbcf82e77cf2f544b9 (patch)
treee1c4e899d73fc4fc17187e8fe8697bdd4c6ed6c3
parent67ec78157631f4d2eb9d737b5c689fd51b735880 (diff)
downloadelgg-80462b4952c73923a369dfdbcf82e77cf2f544b9.tar.gz
elgg-80462b4952c73923a369dfdbcf82e77cf2f544b9.tar.bz2
Marcus Povey <marcus@dushka.co.uk>
* Metadata and relationships can now be accessed as arrays git-svn-id: https://code.elgg.org/elgg/trunk@536 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/lib/extender.php72
-rw-r--r--engine/lib/relationships.php76
2 files changed, 145 insertions, 3 deletions
diff --git a/engine/lib/extender.php b/engine/lib/extender.php
index 9762526aa..e1d42efd6 100644
--- a/engine/lib/extender.php
+++ b/engine/lib/extender.php
@@ -18,7 +18,10 @@
* @package Elgg
* @subpackage Core
*/
- abstract class ElggExtender implements Exportable
+ abstract class ElggExtender implements
+ Exportable,
+ Iterator, // Override foreach behaviour
+ ArrayAccess // Override for array access
{
/**
* This contains the site's main properties (id, etc)
@@ -100,6 +103,8 @@
return can_edit_extender($this->id,$this->type,$user_guid);
}
+ // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
+
/**
* Export this object
*
@@ -115,6 +120,71 @@
return $meta;
}
+
+ // 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;
+ }
+
+ // ARRAY ACCESS INTERFACE //////////////////////////////////////////////////////////
+ /*
+ * This lets an entity's attributes be accessed like an associative array.
+ * Example: http://www.sitepoint.com/print/php5-standard-library
+ */
+
+ function offsetSet($key, $value)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ $this->attributes[$key] = $value;
+ }
+ }
+
+ function offsetGet($key)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ return $this->attributes[$key];
+ }
+ }
+
+ function offsetUnset($key)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ $this->attributes[$key] = ""; // Full unsetting is dangerious for our objects
+ }
+ }
+
+ function offsetExists($offset)
+ {
+ return array_key_exists($offset, $this->attributes);
+ }
}
/**
diff --git a/engine/lib/relationships.php b/engine/lib/relationships.php
index 592e3ecea..c1d2af71f 100644
--- a/engine/lib/relationships.php
+++ b/engine/lib/relationships.php
@@ -18,7 +18,11 @@
* @package Elgg
* @subpackage Core
*/
- class ElggRelationship implements Importable, Exportable
+ class ElggRelationship implements
+ Importable,
+ Exportable,
+ Iterator, // Override foreach behaviour
+ ArrayAccess // Override for array access
{
/**
* This contains the site's main properties (id, etc)
@@ -103,7 +107,8 @@
return delete_relationship($this->id);
}
-
+ // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
+
/**
* Export this relationship
*
@@ -122,6 +127,8 @@
);
}
+ // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
+
/**
* Import a relationship
*
@@ -163,6 +170,71 @@
}
}
}
+
+ // 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;
+ }
+
+ // ARRAY ACCESS INTERFACE //////////////////////////////////////////////////////////
+ /*
+ * This lets an entity's attributes be accessed like an associative array.
+ * Example: http://www.sitepoint.com/print/php5-standard-library
+ */
+
+ function offsetSet($key, $value)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ $this->attributes[$key] = $value;
+ }
+ }
+
+ function offsetGet($key)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ return $this->attributes[$key];
+ }
+ }
+
+ function offsetUnset($key)
+ {
+ if ( array_key_exists($key, $this->attributes) ) {
+ $this->attributes[$key] = ""; // Full unsetting is dangerious for our objects
+ }
+ }
+
+ function offsetExists($offset)
+ {
+ return array_key_exists($offset, $this->attributes);
+ }
}