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 / Model / ViewModel.php
blob8d17894ae328d653f11e7942125cc8ede6db35d6
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\Model;
12 use ArrayAccess;
13 use ArrayIterator;
14 use Traversable;
15 use Zend\Stdlib\ArrayUtils;
16 use Zend\View\Exception;
17 use Zend\View\Model;
18 use Zend\View\Variables as ViewVariables;
20 class ViewModel implements ModelInterface, ClearableModelInterface
22 /**
23 * What variable a parent model should capture this model to
25 * @var string
27 protected $captureTo = 'content';
29 /**
30 * Child models
31 * @var array
33 protected $children = array();
35 /**
36 * Renderer options
37 * @var array
39 protected $options = array();
41 /**
42 * Template to use when rendering this model
44 * @var string
46 protected $template = '';
48 /**
49 * Is this a standalone, or terminal, model?
51 * @var bool
53 protected $terminate = false;
55 /**
56 * View variables
57 * @var array|ArrayAccess&Traversable
59 protected $variables = array();
62 /**
63 * Is this append to child with the same capture?
65 * @var bool
67 protected $append = false;
69 /**
70 * Constructor
72 * @param null|array|Traversable $variables
73 * @param array|Traversable $options
75 public function __construct($variables = null, $options = null)
77 if (null === $variables) {
78 $variables = new ViewVariables();
81 // Initializing the variables container
82 $this->setVariables($variables, true);
84 if (null !== $options) {
85 $this->setOptions($options);
89 /**
90 * Property overloading: set variable value
92 * @param string $name
93 * @param mixed $value
94 * @return void
96 public function __set($name, $value)
98 $this->setVariable($name, $value);
102 * Property overloading: get variable value
104 * @param string $name
105 * @return mixed
107 public function __get($name)
109 if (!$this->__isset($name)) {
110 return null;
113 $variables = $this->getVariables();
114 return $variables[$name];
118 * Property overloading: do we have the requested variable value?
120 * @param string $name
121 * @return bool
123 public function __isset($name)
125 $variables = $this->getVariables();
126 return isset($variables[$name]);
130 * Property overloading: unset the requested variable
132 * @param string $name
133 * @return void
135 public function __unset($name)
137 if (!$this->__isset($name)) {
138 return null;
141 unset($this->variables[$name]);
145 * Set a single option
147 * @param string $name
148 * @param mixed $value
149 * @return ViewModel
151 public function setOption($name, $value)
153 $this->options[(string) $name] = $value;
154 return $this;
158 * Get a single option
160 * @param string $name The option to get.
161 * @param mixed|null $default (optional) A default value if the option is not yet set.
162 * @return mixed
164 public function getOption($name, $default = null)
166 $name = (string) $name;
167 return array_key_exists($name, $this->options) ? $this->options[$name] : $default;
171 * Set renderer options/hints en masse
173 * @param array|Traversable $options
174 * @throws \Zend\View\Exception\InvalidArgumentException
175 * @return ViewModel
177 public function setOptions($options)
179 // Assumption is that lowest common denominator for renderer configuration
180 // is an array
181 if ($options instanceof Traversable) {
182 $options = ArrayUtils::iteratorToArray($options);
185 if (!is_array($options)) {
186 throw new Exception\InvalidArgumentException(sprintf(
187 '%s: expects an array, or Traversable argument; received "%s"',
188 __METHOD__,
189 (is_object($options) ? get_class($options) : gettype($options))
193 $this->options = $options;
194 return $this;
198 * Get renderer options/hints
200 * @return array
202 public function getOptions()
204 return $this->options;
208 * Clear any existing renderer options/hints
210 * @return ViewModel
212 public function clearOptions()
214 $this->options = array();
215 return $this;
219 * Get a single view variable
221 * @param string $name
222 * @param mixed|null $default (optional) default value if the variable is not present.
223 * @return mixed
225 public function getVariable($name, $default = null)
227 $name = (string) $name;
228 if (array_key_exists($name, $this->variables)) {
229 return $this->variables[$name];
232 return $default;
236 * Set view variable
238 * @param string $name
239 * @param mixed $value
240 * @return ViewModel
242 public function setVariable($name, $value)
244 $this->variables[(string) $name] = $value;
245 return $this;
249 * Set view variables en masse
251 * Can be an array or a Traversable + ArrayAccess object.
253 * @param array|ArrayAccess|Traversable $variables
254 * @param bool $overwrite Whether or not to overwrite the internal container with $variables
255 * @throws Exception\InvalidArgumentException
256 * @return ViewModel
258 public function setVariables($variables, $overwrite = false)
260 if (!is_array($variables) && !$variables instanceof Traversable) {
261 throw new Exception\InvalidArgumentException(sprintf(
262 '%s: expects an array, or Traversable argument; received "%s"',
263 __METHOD__,
264 (is_object($variables) ? get_class($variables) : gettype($variables))
268 if ($overwrite) {
269 if (is_object($variables) && !$variables instanceof ArrayAccess) {
270 $variables = ArrayUtils::iteratorToArray($variables);
273 $this->variables = $variables;
274 return $this;
277 foreach ($variables as $key => $value) {
278 $this->setVariable($key, $value);
281 return $this;
285 * Get view variables
287 * @return array|ArrayAccess|Traversable
289 public function getVariables()
291 return $this->variables;
295 * Clear all variables
297 * Resets the internal variable container to an empty container.
299 * @return ViewModel
301 public function clearVariables()
303 $this->variables = new ViewVariables();
304 return $this;
308 * Set the template to be used by this model
310 * @param string $template
311 * @return ViewModel
313 public function setTemplate($template)
315 $this->template = (string) $template;
316 return $this;
320 * Get the template to be used by this model
322 * @return string
324 public function getTemplate()
326 return $this->template;
330 * Add a child model
332 * @param ModelInterface $child
333 * @param null|string $captureTo Optional; if specified, the "capture to" value to set on the child
334 * @param null|bool $append Optional; if specified, append to child with the same capture
335 * @return ViewModel
337 public function addChild(ModelInterface $child, $captureTo = null, $append = null)
339 $this->children[] = $child;
340 if (null !== $captureTo) {
341 $child->setCaptureTo($captureTo);
343 if (null !== $append) {
344 $child->setAppend($append);
347 return $this;
351 * Return all children.
353 * Return specifies an array, but may be any iterable object.
355 * @return array
357 public function getChildren()
359 return $this->children;
363 * Does the model have any children?
365 * @return bool
367 public function hasChildren()
369 return (0 < count($this->children));
373 * Clears out all child models
375 * @return ViewModel
377 public function clearChildren()
379 $this->children = array();
380 return $this;
384 * Set the name of the variable to capture this model to, if it is a child model
386 * @param string $capture
387 * @return ViewModel
389 public function setCaptureTo($capture)
391 $this->captureTo = (string) $capture;
392 return $this;
396 * Get the name of the variable to which to capture this model
398 * @return string
400 public function captureTo()
402 return $this->captureTo;
406 * Set flag indicating whether or not this is considered a terminal or standalone model
408 * @param bool $terminate
409 * @return ViewModel
411 public function setTerminal($terminate)
413 $this->terminate = (bool) $terminate;
414 return $this;
418 * Is this considered a terminal or standalone model?
420 * @return bool
422 public function terminate()
424 return $this->terminate;
428 * Set flag indicating whether or not append to child with the same capture
430 * @param bool $append
431 * @return ViewModel
433 public function setAppend($append)
435 $this->append = (bool) $append;
436 return $this;
440 * Is this append to child with the same capture?
442 * @return bool
444 public function isAppend()
446 return $this->append;
450 * Return count of children
452 * @return int
454 public function count()
456 return count($this->children);
460 * Get iterator of children
462 * @return ArrayIterator
464 public function getIterator()
466 return new ArrayIterator($this->children);