PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/lib-uoguelph-ca/ocs
PHP | 100 lines | 37 code | 11 blank | 52 comment | 8 complexity | 5fffc5e39060f7002e570ff4dcbab2ac MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/filter/GenericMultiplexerFilter.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 GenericMultiplexerFilter
  9. * @ingroup filter
  10. *
  11. * @brief A generic filter that is configured with a number of
  12. * equal type filters. It takes the input argument, applies all
  13. * given filters to it and returns an array of outputs as a result.
  14. *
  15. * The result can then be sent to either an iterator filter or
  16. * to a de-multiplexer filter.
  17. */
  18. // $Id$
  19. import('filter.Filter');
  20. class GenericMultiplexerFilter extends Filter {
  21. /** @var array An unordered array of filters that we run the input over */
  22. var $_filters = array();
  23. /**
  24. * Constructor
  25. */
  26. function GenericMultiplexerFilter() {
  27. parent::Filter();
  28. }
  29. //
  30. // Public methods
  31. //
  32. /**
  33. * Adds a filter to the filter list.
  34. * @param $filter Filter
  35. */
  36. function addFilter(&$filter) {
  37. assert(is_a($filter, 'Filter'));
  38. $this->_filters[] =& $filter;
  39. }
  40. //
  41. // Implementing abstract template methods from Filter
  42. //
  43. /**
  44. * @see Filter::supports()
  45. * @param $input mixed
  46. * @param $output mixed
  47. * @return boolean
  48. */
  49. function supports(&$input, &$output) {
  50. // Preliminary check: do we have filters at all?
  51. if(!count($this->_filters)) return false;
  52. if (!is_null($output)) {
  53. // Pre-check the number of the output objects
  54. if (!is_array($output) || count($output) != count($this->_filters)) return false;
  55. }
  56. // Iterate over the filters and check the
  57. // corresponding inputs and outputs.
  58. foreach($this->_filters as $outputNumber => $filter) {
  59. $currentOutput = (is_null($output) ? null : $output[$outputNumber]);
  60. if (!$filter->supports($input, $currentOutput)) return false;
  61. }
  62. // If all individual filter validations passed then this
  63. // filter supports the given data
  64. return true;
  65. }
  66. /**
  67. * @see Filter::process()
  68. * @param $input mixed
  69. * @return array
  70. */
  71. function &process(&$input) {
  72. // Iterate over all filters and return the results
  73. // as an array.
  74. $output = array();
  75. foreach($this->_filters as $filter) {
  76. // Make a copy of the input so that the filters don't interfere
  77. // with each other.
  78. if (is_object($input)) {
  79. $clonedInput =& cloneObject($input);
  80. } else {
  81. $clonedInput = $input;
  82. }
  83. $output[] = $filter->execute($clonedInput);
  84. unset ($clonedInput);
  85. }
  86. return $output;
  87. }
  88. }
  89. ?>