3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Resolver
;
13 use IteratorAggregate
;
15 use Zend\Stdlib\ArrayUtils
;
16 use Zend\View\Exception
;
17 use Zend\View\Renderer\RendererInterface
as Renderer
;
19 class TemplateMapResolver
implements IteratorAggregate
, ResolverInterface
24 protected $map = array();
29 * Instantiate and optionally populate template map.
31 * @param array|Traversable $map
33 public function __construct($map = array())
39 * IteratorAggregate: return internal iterator
43 public function getIterator()
45 return new ArrayIterator($this->map
);
49 * Set (overwrite) template map
51 * Maps should be arrays or Traversable objects with name => path pairs
53 * @param array|Traversable $map
54 * @throws Exception\InvalidArgumentException
55 * @return TemplateMapResolver
57 public function setMap($map)
59 if (!is_array($map) && !$map instanceof Traversable
) {
60 throw new Exception\
InvalidArgumentException(sprintf(
61 '%s: expects an array or Traversable, received "%s"',
63 (is_object($map) ?
get_class($map) : gettype($map))
67 if ($map instanceof Traversable
) {
68 $map = ArrayUtils
::iteratorToArray($map);
76 * Add an entry to the map
78 * @param string|array|Traversable $nameOrMap
79 * @param null|string $path
80 * @throws Exception\InvalidArgumentException
81 * @return TemplateMapResolver
83 public function add($nameOrMap, $path = null)
85 if (is_array($nameOrMap) ||
$nameOrMap instanceof Traversable
) {
86 $this->merge($nameOrMap);
90 if (!is_string($nameOrMap)) {
91 throw new Exception\
InvalidArgumentException(sprintf(
92 '%s: expects a string, array, or Traversable for the first argument; received "%s"',
94 (is_object($nameOrMap) ?
get_class($nameOrMap) : gettype($nameOrMap))
99 if (isset($this->map
[$nameOrMap])) {
100 unset($this->map
[$nameOrMap]);
105 $this->map
[$nameOrMap] = $path;
110 * Merge internal map with provided map
112 * @param array|Traversable $map
113 * @throws Exception\InvalidArgumentException
114 * @return TemplateMapResolver
116 public function merge($map)
118 if (!is_array($map) && !$map instanceof Traversable
) {
119 throw new Exception\
InvalidArgumentException(sprintf(
120 '%s: expects an array or Traversable, received "%s"',
122 (is_object($map) ?
get_class($map) : gettype($map))
126 if ($map instanceof Traversable
) {
127 $map = ArrayUtils
::iteratorToArray($map);
130 $this->map
= array_replace_recursive($this->map
, $map);
135 * Does the resolver contain an entry for the given name?
137 * @param string $name
140 public function has($name)
142 return array_key_exists($name, $this->map
);
146 * Retrieve a template path by name
148 * @param string $name
149 * @return false|string
150 * @throws Exception\DomainException if no entry exists
152 public function get($name)
154 if (!$this->has($name)) {
157 return $this->map
[$name];
161 * Retrieve the template map
165 public function getMap()
171 * Resolve a template/pattern name to a resource the renderer can consume
173 * @param string $name
174 * @param null|Renderer $renderer
177 public function resolve($name, Renderer
$renderer = null)
179 return $this->get($name);