ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-cvs
php-cvs
[PHP-CVS] cvs: php-src(PHP_5_2) /ext/spl/examples callbackfilteriterator.inc
by Marcus Boerger other posts by this author
Nov 28 2006 2:20PM messages near this date
[PHP-CVS] cvs: php-src / unicode-progress.txt /ext/posix CREDITS /ext/standard credits.c credits_ext.h info.c info.h | [PHP-CVS] cvs: php-src /ext/spl/examples callbackfilteriterator.inc
helly		Tue Nov 28 22:20:01 2006 UTC

  Added files:                 (Branch: PHP_5_2)
    /php-src/ext/spl/examples	callbackfilteriterator.inc 
  Log:
  - MFH: Add new example
  

http://cvs.php.net/viewvc.cgi/php-src/ext/spl/examples/callbackfilteriterator.inc?view=marku
p&rev=1.1
Index: php-src/ext/spl/examples/callbackfilteriterator.inc
+++ php-src/ext/spl/examples/callbackfilteriterator.inc
<?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;
	}
}

?> 

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved