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