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;
13
14use Assetic\Filter\FilterInterface;
15
16/**
17 * Manages the available filters.
18 *
19 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
20 */
21class FilterManager
22{
23    private $filters = array();
24
25    public function set($alias, FilterInterface $filter)
26    {
27        $this->checkName($alias);
28
29        $this->filters[$alias] = $filter;
30    }
31
32    public function get($alias)
33    {
34        if (!isset($this->filters[$alias])) {
35            throw new \InvalidArgumentException(sprintf('There is no "%s" filter.', $alias));
36        }
37
38        return $this->filters[$alias];
39    }
40
41    public function has($alias)
42    {
43        return isset($this->filters[$alias]);
44    }
45
46    public function getNames()
47    {
48        return array_keys($this->filters);
49    }
50
51    /**
52     * Checks that a name is valid.
53     *
54     * @param string $name An asset name candidate
55     *
56     * @throws \InvalidArgumentException If the asset name is invalid
57     */
58    protected function checkName($name)
59    {
60        if (!ctype_alnum(str_replace('_', '', $name))) {
61            throw new \InvalidArgumentException(sprintf('The name "%s" is invalid.', $name));
62        }
63    }
64}
65