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\Model
;
15 use Zend\Stdlib\ArrayUtils
;
16 use Zend\View\Exception
;
18 use Zend\View\Variables
as ViewVariables
;
20 class ViewModel
implements ModelInterface
, ClearableModelInterface
23 * What variable a parent model should capture this model to
27 protected $captureTo = 'content';
33 protected $children = array();
39 protected $options = array();
42 * Template to use when rendering this model
46 protected $template = '';
49 * Is this a standalone, or terminal, model?
53 protected $terminate = false;
57 * @var array|ArrayAccess&Traversable
59 protected $variables = array();
63 * Is this append to child with the same capture?
67 protected $append = false;
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);
90 * Property overloading: set variable value
96 public function __set($name, $value)
98 $this->setVariable($name, $value);
102 * Property overloading: get variable value
104 * @param string $name
107 public function __get($name)
109 if (!$this->__isset($name)) {
113 $variables = $this->getVariables();
114 return $variables[$name];
118 * Property overloading: do we have the requested variable value?
120 * @param string $name
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
135 public function __unset($name)
137 if (!$this->__isset($name)) {
141 unset($this->variables
[$name]);
145 * Set a single option
147 * @param string $name
148 * @param mixed $value
151 public function setOption($name, $value)
153 $this->options
[(string) $name] = $value;
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.
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
177 public function setOptions($options)
179 // Assumption is that lowest common denominator for renderer configuration
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"',
189 (is_object($options) ?
get_class($options) : gettype($options))
193 $this->options
= $options;
198 * Get renderer options/hints
202 public function getOptions()
204 return $this->options
;
208 * Clear any existing renderer options/hints
212 public function clearOptions()
214 $this->options
= array();
219 * Get a single view variable
221 * @param string $name
222 * @param mixed|null $default (optional) default value if the variable is not present.
225 public function getVariable($name, $default = null)
227 $name = (string) $name;
228 if (array_key_exists($name, $this->variables
)) {
229 return $this->variables
[$name];
238 * @param string $name
239 * @param mixed $value
242 public function setVariable($name, $value)
244 $this->variables
[(string) $name] = $value;
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
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"',
264 (is_object($variables) ?
get_class($variables) : gettype($variables))
269 if (is_object($variables) && !$variables instanceof ArrayAccess
) {
270 $variables = ArrayUtils
::iteratorToArray($variables);
273 $this->variables
= $variables;
277 foreach ($variables as $key => $value) {
278 $this->setVariable($key, $value);
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.
301 public function clearVariables()
303 $this->variables
= new ViewVariables();
308 * Set the template to be used by this model
310 * @param string $template
313 public function setTemplate($template)
315 $this->template
= (string) $template;
320 * Get the template to be used by this model
324 public function getTemplate()
326 return $this->template
;
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
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);
351 * Return all children.
353 * Return specifies an array, but may be any iterable object.
357 public function getChildren()
359 return $this->children
;
363 * Does the model have any children?
367 public function hasChildren()
369 return (0 < count($this->children
));
373 * Clears out all child models
377 public function clearChildren()
379 $this->children
= array();
384 * Set the name of the variable to capture this model to, if it is a child model
386 * @param string $capture
389 public function setCaptureTo($capture)
391 $this->captureTo
= (string) $capture;
396 * Get the name of the variable to which to capture this model
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
411 public function setTerminal($terminate)
413 $this->terminate
= (bool) $terminate;
418 * Is this considered a terminal or standalone model?
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
433 public function setAppend($append)
435 $this->append
= (bool) $append;
440 * Is this append to child with the same capture?
444 public function isAppend()
446 return $this->append
;
450 * Return count of children
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
);