PageRenderTime 1371ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/lib-uoguelph-ca/ocs
PHP | 123 lines | 46 code | 15 blank | 62 comment | 5 complexity | 65941b30fdd65054a79c801804581151 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/filter/GenericSequencerFilter.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class GenericSequencerFilter
  9. * @ingroup filter
  10. *
  11. * @brief A generic filter that is configured with a number of
  12. * ordered filters. It takes the input argument of the first filter,
  13. * passes its output to the next filter and so on and finally returns
  14. * the result of the last filter in the chain to the caller.
  15. */
  16. // $Id$
  17. import('filter.Filter');
  18. class GenericSequencerFilter extends Filter {
  19. /** @var array An array of filters that we run in order */
  20. var $_filters = array();
  21. /** @var array test objects required for filter chain validation */
  22. var $_intermediateResultSamples = array();
  23. /**
  24. * Constructor
  25. */
  26. function GenericSequencerFilter() {
  27. parent::Filter();
  28. }
  29. //
  30. // Public methods
  31. //
  32. /**
  33. * Adds a filter to the end of the
  34. * filter list.
  35. * @param $filter Filter
  36. * @param $inputSample mixed a test object that validates as input against
  37. * the supports() function of the added filter and also has to be supported
  38. * as output of the previously added filter (if any). This will be used
  39. * to validate the filter sequence.
  40. */
  41. function addFilter(&$filter, &$inputSample) {
  42. assert(is_a($filter, 'Filter'));
  43. assert(!is_null($inputSample));
  44. // The sample must be supported as input by the added
  45. // filter
  46. assert($filter->supportsAsInput($inputSample));
  47. // The sample must be supported as output by the
  48. // previously added filter (if there is any).
  49. $previouslyAddedFilterId = count($this->_filters)-1;
  50. if ($previouslyAddedFilterId >= 0) {
  51. $previousFilter =& $this->_filters[$previouslyAddedFilterId];
  52. $previousInputSample =& $this->_intermediateResultSamples[$previouslyAddedFilterId];
  53. assert($previousFilter->supports($previousInputSample, $inputSample));
  54. }
  55. // Store filter and sample data
  56. $this->_intermediateResultSamples[] =& $inputSample;
  57. $this->_filters[] =& $filter;
  58. }
  59. //
  60. // Implementing abstract template methods from Filter
  61. //
  62. /**
  63. * @see Filter::supports()
  64. * @param $input mixed
  65. * @param $output mixed
  66. * @return boolean
  67. */
  68. function supports(&$input, &$output) {
  69. // Preliminary check: do we have filters at all?
  70. if(!count($this->_filters)) return false;
  71. $nullVar = null;
  72. // The input must be validated by the first filter
  73. // in the sequence.
  74. $firstFilter =& $this->_filters[0];
  75. if (!$firstFilter->supports($input, $nullVar)) return false;
  76. // The output must be validated by the last filter
  77. // in the sequence.
  78. $lastFilterId = count($this->_filters)-1;
  79. $lastFilter =& $this->_filters[$lastFilterId];
  80. $inputSample =& $this->_intermediateResultSamples[$lastFilterId];
  81. return $lastFilter->supports($inputSample, $output);
  82. }
  83. /**
  84. * @see Filter::process()
  85. * @param $input mixed
  86. * @return mixed
  87. */
  88. function &process(&$input) {
  89. // Iterate over all filters and always feed the
  90. // output of one filter as input to the next
  91. // filter.
  92. $previousOutput = null;
  93. foreach($this->_filters as $filter) {
  94. if(is_null($previousOutput)) {
  95. // First filter
  96. $previousOutput =& $input;
  97. }
  98. $output = $filter->execute($previousOutput);
  99. // If one filter returns null then we'll abort
  100. // execution of the filter chain.
  101. if (is_null($output)) break;
  102. unset($previousOutput);
  103. $previousOutput = $output;
  104. }
  105. return $output;
  106. }
  107. }
  108. ?>