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
15 * Abstract class for Zend_View to help enforce private constructs.
17 * @todo Allow specifying string names for manager, filter chain, variables
18 * @todo Move escaping into variables object
19 * @todo Move strict variables into variables object
21 class Variables
extends ArrayObject
24 * Strict variables flag; when on, undefined variables accessed in the view
25 * scripts will trigger notices
29 protected $strictVars = false;
34 * @param array $variables
35 * @param array $options
37 public function __construct(array $variables = array(), array $options = array())
41 ArrayObject
::ARRAY_AS_PROPS
,
45 $this->setOptions($options);
51 * @param array $options
54 public function setOptions(array $options)
56 foreach ($options as $key => $value) {
57 switch (strtolower($key)) {
59 $this->setStrictVars($value);
62 // Unknown options are considered variables
71 * Set status of "strict vars" flag
76 public function setStrictVars($flag)
78 $this->strictVars
= (bool) $flag;
83 * Are we operating with strict variables?
87 public function isStrict()
89 return $this->strictVars
;
93 * Assign many values at once
95 * @param array|object $spec
97 * @throws Exception\InvalidArgumentException
99 public function assign($spec)
101 if (is_object($spec)) {
102 if (method_exists($spec, 'toArray')) {
103 $spec = $spec->toArray();
105 $spec = (array) $spec;
108 if (!is_array($spec)) {
109 throw new Exception\
InvalidArgumentException(sprintf(
110 'assign() expects either an array or an object as an argument; received "%s"',
114 foreach ($spec as $key => $value) {
115 $this[$key] = $value;
122 * Get the variable value
124 * If the value has not been defined, a null value will be returned; if
125 * strict vars on in place, a notice will also be raised.
127 * Otherwise, returns _escaped_ version of the value.
132 public function offsetGet($key)
134 if (!$this->offsetExists($key)) {
135 if ($this->isStrict()) {
136 trigger_error(sprintf(
137 'View variable "%s" does not exist', $key
143 $return = parent
::offsetGet($key);
145 // If we have a closure/functor, invoke it, and return its return value
146 if (is_object($return) && is_callable($return)) {
147 $return = call_user_func($return);
154 * Clear all variables
158 public function clear()
160 $this->exchangeArray(array());