ocs /lib/pkp/classes/filter/GenericSequencerFilter.inc.php

Language PHP Lines 123
MD5 Hash 65941b30fdd65054a79c801804581151
Repository https://github.com/lib-uoguelph-ca/ocs.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
 * @file classes/filter/GenericSequencerFilter.inc.php
 *
 * Copyright (c) 2000-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class GenericSequencerFilter
 * @ingroup filter
 *
 * @brief A generic filter that is configured with a number of
 *  ordered filters. It takes the input argument of the first filter,
 *  passes its output to the next filter and so on and finally returns
 *  the result of the last filter in the chain to the caller.
 */

// $Id$

import('filter.Filter');

class GenericSequencerFilter extends Filter {
	/** @var array An array of filters that we run in order */
	var $_filters = array();

	/** @var array test objects required for filter chain validation */
	var $_intermediateResultSamples = array();

	/**
	 * Constructor
	 */
	function GenericSequencerFilter() {
		parent::Filter();
	}

	//
	// Public methods
	//
	/**
	 * Adds a filter to the end of the
	 * filter list.
	 * @param $filter Filter
	 * @param $inputSample mixed a test object that validates as input against
	 *  the supports() function of the added filter and also has to be supported
	 *  as output of the previously added filter (if any). This will be used
	 *  to validate the filter sequence.
	 */
	function addFilter(&$filter, &$inputSample) {
		assert(is_a($filter, 'Filter'));
		assert(!is_null($inputSample));

		// The sample must be supported as input by the added
		// filter
		assert($filter->supportsAsInput($inputSample));

		// The sample must be supported as output by the
		// previously added filter (if there is any).
		$previouslyAddedFilterId = count($this->_filters)-1;
		if ($previouslyAddedFilterId >= 0) {
			$previousFilter =& $this->_filters[$previouslyAddedFilterId];
			$previousInputSample =& $this->_intermediateResultSamples[$previouslyAddedFilterId];
			assert($previousFilter->supports($previousInputSample, $inputSample));
		}

		// Store filter and sample data
		$this->_intermediateResultSamples[] =& $inputSample;
		$this->_filters[] =& $filter;
	}

	//
	// Implementing abstract template methods from Filter
	//
	/**
	 * @see Filter::supports()
	 * @param $input mixed
	 * @param $output mixed
	 * @return boolean
	 */
	function supports(&$input, &$output) {
		// Preliminary check: do we have filters at all?
		if(!count($this->_filters)) return false;
		$nullVar = null;

		// The input must be validated by the first filter
		// in the sequence.
		$firstFilter =& $this->_filters[0];
		if (!$firstFilter->supports($input, $nullVar)) return false;

		// The output must be validated by the last filter
		// in the sequence.
		$lastFilterId = count($this->_filters)-1;
		$lastFilter =& $this->_filters[$lastFilterId];
		$inputSample =& $this->_intermediateResultSamples[$lastFilterId];
		return $lastFilter->supports($inputSample, $output);
	}

	/**
	 * @see Filter::process()
	 * @param $input mixed
	 * @return mixed
	 */
	function &process(&$input) {
		// Iterate over all filters and always feed the
		// output of one filter as input to the next
		// filter.
		$previousOutput = null;
		foreach($this->_filters as $filter) {
			if(is_null($previousOutput)) {
				// First filter
				$previousOutput =& $input;
			}
			$output = $filter->execute($previousOutput);

			// If one filter returns null then we'll abort
			// execution of the filter chain.
			if (is_null($output)) break;

			unset($previousOutput);
			$previousOutput = $output;
		}
		return $output;
	}
}
?>
Back to Top