FilterCollection.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. /**
  13. * A collection of filters.
  14. *
  15. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  16. */
  17. class FilterCollection implements FilterInterface, \IteratorAggregate, \Countable
  18. {
  19. private $filters = array();
  20. public function __construct($filters = array())
  21. {
  22. foreach ($filters as $filter) {
  23. $this->ensure($filter);
  24. }
  25. }
  26. /**
  27. * Checks that the current collection contains the supplied filter.
  28. *
  29. * If the supplied filter is another filter collection, each of its
  30. * filters will be checked.
  31. */
  32. public function ensure(FilterInterface $filter)
  33. {
  34. if ($filter instanceof \Traversable) {
  35. foreach ($filter as $f) {
  36. $this->ensure($f);
  37. }
  38. } elseif (!in_array($filter, $this->filters, true)) {
  39. $this->filters[] = $filter;
  40. }
  41. }
  42. public function all()
  43. {
  44. return $this->filters;
  45. }
  46. public function clear()
  47. {
  48. $this->filters = array();
  49. }
  50. public function filterLoad(AssetInterface $asset)
  51. {
  52. foreach ($this->filters as $filter) {
  53. $filter->filterLoad($asset);
  54. }
  55. }
  56. public function filterDump(AssetInterface $asset)
  57. {
  58. foreach ($this->filters as $filter) {
  59. $filter->filterDump($asset);
  60. }
  61. }
  62. public function getIterator()
  63. {
  64. return new \ArrayIterator($this->filters);
  65. }
  66. public function count()
  67. {
  68. return count($this->filters);
  69. }
  70. }