FilterManager.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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;
  11. use Assetic\Filter\FilterInterface;
  12. /**
  13. * Manages the available filters.
  14. *
  15. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  16. */
  17. class FilterManager
  18. {
  19. private $filters = array();
  20. public function set($alias, FilterInterface $filter)
  21. {
  22. $this->checkName($alias);
  23. $this->filters[$alias] = $filter;
  24. }
  25. public function get($alias)
  26. {
  27. if (!isset($this->filters[$alias])) {
  28. throw new \InvalidArgumentException(sprintf('There is no "%s" filter.', $alias));
  29. }
  30. return $this->filters[$alias];
  31. }
  32. public function has($alias)
  33. {
  34. return isset($this->filters[$alias]);
  35. }
  36. public function getNames()
  37. {
  38. return array_keys($this->filters);
  39. }
  40. /**
  41. * Checks that a name is valid.
  42. *
  43. * @param string $name An asset name candidate
  44. *
  45. * @throws InvalidArgumentException If the asset name is invalid
  46. */
  47. protected function checkName($name)
  48. {
  49. if (!ctype_alnum(str_replace('_', '', $name))) {
  50. throw new \InvalidArgumentException(sprintf('The name "%s" is invalid.', $name));
  51. }
  52. }
  53. }