PluginProbe
GetResponse Forms by Optin Cat / 1.5.4
GetResponse Forms by Optin Cat v1.5.4
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / classes / Mustache / Context.php

Context.php in GetResponse Forms by Optin Cat 1.5.4, at includes/classes/Mustache/Context.php

155 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Mustache.php.
5 *
6 * (c) 2010-2014 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 /**
13 * Mustache Template rendering Context.
14 */
15 class Mustache_Context
16 {
17 private $stack = array();
18
19 /**
20 * Mustache rendering Context constructor.
21 *
22 * @param mixed $context Default rendering context (default: null)
23 */
24 public function __construct($context = null)
25 {
26 if ($context !== null) {
27 $this->stack = array($context);
28 }
29 }
30
31 /**
32 * Push a new Context frame onto the stack.
33 *
34 * @param mixed $value Object or array to use for context
35 */
36 public function push($value)
37 {
38 array_push($this->stack, $value);
39 }
40
41 /**
42 * Pop the last Context frame from the stack.
43 *
44 * @return mixed Last Context frame (object or array)
45 */
46 public function pop()
47 {
48 return array_pop($this->stack);
49 }
50
51 /**
52 * Get the last Context frame.
53 *
54 * @return mixed Last Context frame (object or array)
55 */
56 public function last()
57 {
58 return end($this->stack);
59 }
60
61 /**
62 * Find a variable in the Context stack.
63 *
64 * Starting with the last Context frame (the context of the innermost section), and working back to the top-level
65 * rendering context, look for a variable with the given name:
66 *
67 * * If the Context frame is an associative array which contains the key $id, returns the value of that element.
68 * * If the Context frame is an object, this will check first for a public method, then a public property named
69 * $id. Failing both of these, it will try `__isset` and `__get` magic methods.
70 * * If a value named $id is not found in any Context frame, returns an empty string.
71 *
72 * @param string $id Variable name
73 *
74 * @return mixed Variable value, or '' if not found
75 */
76 public function find($id)
77 {
78 return $this->findVariableInStack($id, $this->stack);
79 }
80
81 /**
82 * Find a 'dot notation' variable in the Context stack.
83 *
84 * Note that dot notation traversal bubbles through scope differently than the regular find method. After finding
85 * the initial chunk of the dotted name, each subsequent chunk is searched for only within the value of the previous
86 * result. For example, given the following context stack:
87 *
88 * $data = array(
89 * 'name' => 'Fred',
90 * 'child' => array(
91 * 'name' => 'Bob'
92 * ),
93 * );
94 *
95 * ... and the Mustache following template:
96 *
97 * {{ child.name }}
98 *
99 * ... the `name` value is only searched for within the `child` value of the global Context, not within parent
100 * Context frames.
101 *
102 * @param string $id Dotted variable selector
103 *
104 * @return mixed Variable value, or '' if not found
105 */
106 public function findDot($id)
107 {
108 $chunks = explode('.', $id);
109 $first = array_shift($chunks);
110 $value = $this->findVariableInStack($first, $this->stack);
111
112 foreach ($chunks as $chunk) {
113 if ($value === '') {
114 return $value;
115 }
116
117 $value = $this->findVariableInStack($chunk, array($value));
118 }
119
120 return $value;
121 }
122
123 /**
124 * Helper function to find a variable in the Context stack.
125 *
126 * @see Mustache_Context::find
127 *
128 * @param string $id Variable name
129 * @param array $stack Context stack
130 *
131 * @return mixed Variable value, or '' if not found
132 */
133 private function findVariableInStack($id, array $stack)
134 {
135 for ($i = count($stack) - 1; $i >= 0; $i--) {
136 if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) {
137
138 // Note that is_callable() *will not work here*
139 // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
140 if (method_exists($stack[$i], $id)) {
141 return $stack[$i]->$id();
142 } elseif (isset($stack[$i]->$id)) {
143 return $stack[$i]->$id;
144 } elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) {
145 return $stack[$i][$id];
146 }
147 } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
148 return $stack[$i][$id];
149 }
150 }
151
152 return '';
153 }
154 }
155