<?php

	
	
	/**
	 * \ingroup File
	 *
	 * Uses the DirectoryTreeIterator class to provide a similar output
	 * but optinally using a regular expression or shell pattern to filter
	 * out the list of results.
	 *
	 * @author The LifeType Project
	 */	
	class FilteredDirectoryTreeIterator extends FilterIterator
	{
	  	private $_filter;
		private $_patternMode;

		const SHELL_PATTERN = 1;
		const REGEXP_PATTERN = 2;
	
		/**
		 * Creates a new iterator
		 *
		 * @param path The path where to start generating the folder tree
		 * @param filter The regular expression or shell pattern that will be used to filter files. It can be
		 * an array.
		 * @param mode Either FilteredDirectoryTree::SHELL_PATTERN to indicate that the $filter parameter is a
		 * shell pattern or FilteredDirectoryTree::REGEXP_PATTERN to indicate that we're dealing with
		 * a regular expression
		 * @return 
		 */
		function __construct( $path, $filter, $mode = FilteredDirectoryTreeIterator::SHELL_PATTERN )
	  	{
	    	parent::__construct(
	    		new DirectoryTreeIterator($path)
			);

			if( !is_array( $filter ))
				$this->_filter = Array( $filter );
			else
	    		$this->_filter= $filter;
	
			$this->_mode = $mode;
	  	}

		function accept()
	  	{
			if( $this->_mode == FilteredDirectoryTreeIterator::REGEXP_PATTERN ) {
				foreach( $this->_filter as $pattern ) {
	    			if( preg_match( $pattern, $this->key()))
						return( true );
				}
			}
			else {
				
				foreach( $this->_filter as $pattern ) {
					if( Glob::myFnmatch( $pattern, $this->key()))
						return( true );
				}
			}
			
			return( false );
		}
		
		protected function __clone() 
		{
			return false;
	  	}

		function current()
		{
	    	return parent::key();
		}

		function key()
		{
	    	return parent::key();
		}
	}
?>