diff options
| author | Marcus Boerger <helly@php.net> | 2006-11-28 22:20:01 +0000 |
|---|---|---|
| committer | Marcus Boerger <helly@php.net> | 2006-11-28 22:20:01 +0000 |
| commit | ecd417c6ce6f164d06f1296a0beb2c8b7d6e7312 (patch) | |
| tree | e074574bfa7d9d1252860ff7d223e1e0049211cd /ext/spl/examples | |
| parent | 0966666256fcb95bd13b80be97e0ce4986764a40 (diff) | |
| download | php-git-ecd417c6ce6f164d06f1296a0beb2c8b7d6e7312.tar.gz | |
- MFH: Add new example
Diffstat (limited to 'ext/spl/examples')
| -rwxr-xr-x | ext/spl/examples/callbackfilteriterator.inc | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/ext/spl/examples/callbackfilteriterator.inc b/ext/spl/examples/callbackfilteriterator.inc new file mode 100755 index 0000000000..96a8abfc04 --- /dev/null +++ b/ext/spl/examples/callbackfilteriterator.inc @@ -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 |
