]> granicus.if.org Git - php/commitdiff
- Add new example
authorMarcus Boerger <helly@php.net>
Tue, 28 Nov 2006 22:19:22 +0000 (22:19 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 28 Nov 2006 22:19:22 +0000 (22:19 +0000)
ext/spl/examples/callbackfilteriterator.inc [new file with mode: 0755]

diff --git a/ext/spl/examples/callbackfilteriterator.inc b/ext/spl/examples/callbackfilteriterator.inc
new file mode 100755 (executable)
index 0000000..96a8abf
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+
+/** @file callbackfilteriterator.inc
+ * @ingroup Examples
+ * @brief class CallbackFilterIterator
+ * @author  Marcus Boerger
+ * @author  Kevin McArthur
+ * @date    2006 - 2006
+ *
+ * SPL - Standard PHP Library
+ */
+
+/** @ingroup Examples
+ * @brief   A non abstract FiletrIterator that uses a callback foreach element
+ * @author  Marcus Boerger
+ * @author  Kevin McArthur
+ * @version 1.0
+ */
+class CallbackFilterIterator extends FilterIterator
+{
+       const USE_FALSE = 0;  /**< mode: accept no elements, no callback */
+       const USE_TRUE  = 1;  /**< mode: accept all elements, no callback */
+       const USE_VALUE = 2;  /**< mode: pass value to callback */
+       const USE_KEY   = 3;  /**< mode: pass key to callback */
+       const USE_BOTH  = 4;  /**< mode: pass value and key to callback */
+
+       const REPLACE   = 0x00000001; /**< flag: pass key/value by reference */
+
+       private $callback; /**< callback to use */
+       private $mode;     /**< mode any of USE_VALUE, USE_KEY, USE_BOTH */
+       private $flags;    /**< flags */
+       private $key;      /**< key value */
+       private $current;  /**< current value */
+
+       /** Construct a CallbackFilterIterator
+        *
+        * @param it        inner iterator (iterator to filter)
+        * @param callback  callback function
+        * @param mode      @copy $mode
+        * @param flags     @copy $flags
+        */
+       public function __construct(Iterator $it, $callback, $mode = self::USE_VALUE, $flags = 0)
+       {
+               parent::__construct($it);
+               $this->callback = $callback;
+               $this->mode     = $mode;
+               $this->flags    = $flags;
+       }
+
+       /** Call the filter callback
+        * @return result of filter callback
+        */
+       public function accept()
+       {
+               $this->key     = parent::key();
+               $this->current = parent::current();
+
+               switch($this->mode) {
+               default:
+               case self::USE_FALSE;
+                       return false;
+               case self::USE_TRUE:
+                       return true;
+               case self::USE_VALUE:
+                       if($this->flags & self::REPLACE) {
+                               return (bool) call_user_func($this->callback, &$this->current);
+                       } else {
+                               return (bool) call_user_func($this->callback, $this->current);
+                       }
+               case self::USE_KEY:
+                       if($this->flags & self::REPLACE) {
+                               return (bool) call_user_func($this->callback, &$this->key);
+                       } else {
+                               return (bool) call_user_func($this->callback, $this->key);
+                       }
+               case SELF::USE_BOTH:
+                       if($this->flags & self::REPLACE) {
+                               return (bool) call_user_func($this->callback, &$this->key, &$this->current);
+                       } else {
+                               return (bool) call_user_func($this->callback, $this->key, $this->current);
+                       }
+               }
+       }
+
+       /** @return @copy $key */
+       function key()
+       {
+               return $this->key;
+       }
+
+       /** @return @copy $current */
+       function current()
+       {
+               return $this->current;
+       }
+
+       /** @return @copy $mode */
+       function getMode()
+       {
+               return $this->mode;
+       }
+
+       /** @param $mode set new mode, @see $mode */
+       function setMode($mode)
+       {
+               $this->mode = $mode;
+       }
+
+       /** @return @copy $flags */
+       function getFlags()
+       {
+               return $this->flags;
+       }
+
+       /** @param $flags set new flags, @see @copy $flags */
+       function setFlags($flags)
+       {
+               $this->flags = $flags;
+       }
+}
+
+?>
\ No newline at end of file