Add PlIteratorUtils::getObjectProperty callback
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 8 Mar 2010 01:00:38 +0000 (02:00 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 8 Mar 2010 01:00:38 +0000 (02:00 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/pliteratorutils.php

index db10927..e24ab81 100644 (file)
@@ -105,6 +105,16 @@ class PlIteratorUtils
         $callback = new _GetArrayValueCallback($key);
         return array($callback, 'get');
     }
+
+    /** Returns the callback for '$x -> $x->prop';
+     * @param $property The property to retrieve
+     * @return a callback
+     */
+    public static function arrayValueCallback($property)
+    {
+        $callback = new _GetObjectPropertyCallback($property);
+        return array($callback, 'get');
+    }
 }
 
 /** Iterates over an array.
@@ -550,5 +560,22 @@ class _GetArrayValueCallback
     }
 }
 
+// Wrapper class for 'objectPropertyCallback' (get property ->$blah of the given object)
+class _GetObjectPropertyCallback
+{
+    private $property;
+
+    public function __construct($property)
+    {
+        $this->property = $property;
+    }
+
+    public function get($obj)
+    {
+        $p = $this->property;
+        return @$obj->$p;
+    }
+}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>