Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / View / Resolver / TemplateMapResolver.php
blob6c649def1fdc72c84c86139c8656418ae2ad0f9a
1 <?php
2 /**
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
8 */
10 namespace Zend\View\Resolver;
12 use ArrayIterator;
13 use IteratorAggregate;
14 use Traversable;
15 use Zend\Stdlib\ArrayUtils;
16 use Zend\View\Exception;
17 use Zend\View\Renderer\RendererInterface as Renderer;
19 class TemplateMapResolver implements IteratorAggregate, ResolverInterface
21 /**
22 * @var array
24 protected $map = array();
26 /**
27 * Constructor
29 * Instantiate and optionally populate template map.
31 * @param array|Traversable $map
33 public function __construct($map = array())
35 $this->setMap($map);
38 /**
39 * IteratorAggregate: return internal iterator
41 * @return Traversable
43 public function getIterator()
45 return new ArrayIterator($this->map);
48 /**
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"',
62 __METHOD__,
63 (is_object($map) ? get_class($map) : gettype($map))
64 ));
67 if ($map instanceof Traversable) {
68 $map = ArrayUtils::iteratorToArray($map);
71 $this->map = $map;
72 return $this;
75 /**
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);
87 return $this;
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"',
93 __METHOD__,
94 (is_object($nameOrMap) ? get_class($nameOrMap) : gettype($nameOrMap))
95 ));
98 if (empty($path)) {
99 if (isset($this->map[$nameOrMap])) {
100 unset($this->map[$nameOrMap]);
102 return $this;
105 $this->map[$nameOrMap] = $path;
106 return $this;
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"',
121 __METHOD__,
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);
131 return $this;
135 * Does the resolver contain an entry for the given name?
137 * @param string $name
138 * @return bool
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)) {
155 return false;
157 return $this->map[$name];
161 * Retrieve the template map
163 * @return array
165 public function getMap()
167 return $this->map;
171 * Resolve a template/pattern name to a resource the renderer can consume
173 * @param string $name
174 * @param null|Renderer $renderer
175 * @return string
177 public function resolve($name, Renderer $renderer = null)
179 return $this->get($name);