Fix problem in PlIteratorUtils
[platal.git] / classes / pliteratorutils.php
index eff6eda..dfa8a36 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 objectPropertyCallback($property)
+    {
+        $callback = new _GetObjectPropertyCallback($property);
+        return array($callback, 'get');
+    }
 }
 
 /** Iterates over an array.
@@ -438,6 +448,11 @@ class PlSubIterator implements PlIterator
         return ($this->source->last() && $this->next == null);
     }
 
+    public function first()
+    {
+        return $this->source->first();
+    }
+
     // Called by a subiterator to "rewind" the core iterator
     public function setNext($item)
     {
@@ -518,6 +533,11 @@ class PlInnerSubIterator implements PlIterator
         return $this->over;
     }
 
+    public function first()
+    {
+        return false;
+    }
+
 }
 
 // Wrapper class for 'arrayValueCallback' (get field $key of the given array)
@@ -532,13 +552,30 @@ class _GetArrayValueCallback
 
     public function get(array $arr)
     {
-        if (array_key_exists($key, $arr)) {
-            return $arr[$key];
+        if (array_key_exists($this->key, $arr)) {
+            return $arr[$this->key];
         } else {
             return null;
         }
     }
 }
 
+// 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:
 ?>