HandlebarsContext

Handlebars context Context for a template.

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Context.php  
  1. class Context 
  2.  
  3. /** 
  4. * List of charcters that cannot be used in identifiers. 
  5. */ 
  6. const NOT_VALID_NAME_CHARS = '!"#%&\'()*+, ./;<=>@[\\]^`{|}~'; 
  7.  
  8. /** 
  9. * List of characters that cannot be used in identifiers in segment-literal 
  10. * notation. 
  11. */ 
  12. const NOT_VALID_SEGMENT_NAME_CHARS = "]"; 
  13.  
  14. /** 
  15. * @var array stack for context only top stack is available 
  16. */ 
  17. protected $stack = array(); 
  18.  
  19. /** 
  20. * @var array index stack for sections 
  21. */ 
  22. protected $index = array(); 
  23.  
  24. /** 
  25. * @var array key stack for objects 
  26. */ 
  27. protected $key = array(); 
  28.  
  29. /** 
  30. * @var array Special variables stack for sections. Each stack element can 
  31. * contain elements with "@index", "@key", "@first" and "@last" keys. 
  32. */ 
  33. protected $specialVariables = array(); 
  34.  
  35. /** 
  36. * Mustache rendering Context constructor. 
  37. * @param mixed $context Default rendering context (default: null) 
  38. */ 
  39. public function __construct($context = null) 
  40. if ($context !== null) { 
  41. $this->stack = array($context); 
  42.  
  43. /** 
  44. * Push a new Context frame onto the stack. 
  45. * @param mixed $value Object or array to use for context 
  46. * @return void 
  47. */ 
  48. public function push($value) 
  49. array_push($this->stack, $value); 
  50.  
  51. /** 
  52. * Push an array of special variables to stack. 
  53. * @param array $variables An associative array of special variables. 
  54. * @return void 
  55. * @see \Handlebars\Context::$specialVariables 
  56. */ 
  57. public function pushSpecialVariables($variables) 
  58. array_push($this->specialVariables, $variables); 
  59.  
  60. /** 
  61. * Pop the last Context frame from the stack. 
  62. * @return mixed Last Context frame (object or array) 
  63. */ 
  64. public function pop() 
  65. return array_pop($this->stack); 
  66.  
  67. /** 
  68. * Pop the last special variables set from the stack. 
  69. * @return array Associative array of special variables. 
  70. * @see \Handlebars\Context::$specialVariables 
  71. */ 
  72. public function popSpecialVariables() 
  73. return array_pop($this->specialVariables); 
  74.  
  75. /** 
  76. * Get the last Context frame. 
  77. * @return mixed Last Context frame (object or array) 
  78. */ 
  79. public function last() 
  80. return end($this->stack); 
  81.  
  82. /** 
  83. * Get the last special variables set from the stack. 
  84. * @return array Associative array of special variables. 
  85. * @see \Handlebars\Context::$specialVariables 
  86. */ 
  87. public function lastSpecialVariables() 
  88. return end($this->specialVariables); 
  89.  
  90. /** 
  91. * Change the current context to one of current context members 
  92. * @param string $variableName name of variable or a callable on current context 
  93. * @return mixed actual value 
  94. */ 
  95. public function with($variableName) 
  96. $value = $this->get($variableName); 
  97. $this->push($value); 
  98.  
  99. return $value; 
  100.  
  101. /** 
  102. * Get a available from current context 
  103. * Supported types : 
  104. * variable , ../variable , variable.variable , variable.[variable] , . 
  105. * @param string $variableName variable name to get from current context 
  106. * @param boolean $strict strict search? if not found then throw exception 
  107. * @throws \InvalidArgumentException in strict mode and variable not found 
  108. * @throws \RuntimeException if supplied argument is a malformed quoted string  
  109. * @throws \InvalidArgumentException if variable name is invalid 
  110. * @return mixed 
  111. */ 
  112. public function get($variableName, $strict = false) 
  113. if ($variableName instanceof \Handlebars\String) { 
  114. return (string)$variableName; 
  115. $variableName = trim($variableName); 
  116. $level = 0; 
  117. while (substr($variableName, 0, 3) == '../') { 
  118. $variableName = trim(substr($variableName, 3)); 
  119. $level++; 
  120. if (count($this->stack) < $level) { 
  121. if ($strict) { 
  122. throw new \InvalidArgumentException( 
  123. 'can not find variable in context' 
  124. ); 
  125.  
  126. return ''; 
  127. end($this->stack); 
  128. while ($level) { 
  129. prev($this->stack); 
  130. $level--; 
  131. $current = current($this->stack); 
  132. if (!$variableName) { 
  133. if ($strict) { 
  134. throw new \InvalidArgumentException( 
  135. 'can not find variable in context' 
  136. ); 
  137. return ''; 
  138. } elseif ($variableName == '.' || $variableName == 'this') { 
  139. return $current; 
  140. } elseif ($variableName[0] == '@') { 
  141. $specialVariables = $this->lastSpecialVariables(); 
  142. if (isset($specialVariables[$variableName])) { 
  143. return $specialVariables[$variableName]; 
  144. } elseif ($strict) { 
  145. throw new \InvalidArgumentException( 
  146. 'can not find variable in context' 
  147. ); 
  148. } else { 
  149. return ''; 
  150. } else { 
  151. $chunks = $this->_splitVariableName($variableName); 
  152. foreach ($chunks as $chunk) { 
  153. if (is_string($current) and $current == '') { 
  154. return $current; 
  155. $current = $this->_findVariableInContext($current, $chunk, $strict); 
  156. return $current; 
  157.  
  158. /** 
  159. * Check if $variable->$inside is available 
  160. * @param mixed $variable variable to check 
  161. * @param string $inside property/method to check 
  162. * @param boolean $strict strict search? if not found then throw exception 
  163. * @throws \InvalidArgumentException in strict mode and variable not found 
  164. * @return boolean true if exist 
  165. */ 
  166. private function _findVariableInContext($variable, $inside, $strict = false) 
  167. $value = null; 
  168. if (($inside !== '0' && empty($inside)) || ($inside == 'this')) { 
  169. return $variable; 
  170. } elseif (is_array($variable)) { 
  171. if (isset($variable[$inside]) || array_key_exists($inside, $variable)) { 
  172. return $variable[$inside]; 
  173. } elseif ($inside == "length") { 
  174. return count($variable); 
  175. } elseif (is_object($variable)) { 
  176. if (isset($variable->$inside)) { 
  177. return $variable->$inside; 
  178. } elseif (is_callable(array($variable, $inside))) { 
  179. return call_user_func(array($variable, $inside)); 
  180.  
  181. if ($strict) { 
  182. throw new \InvalidArgumentException('can not find variable in context'); 
  183.  
  184. return $value; 
  185.  
  186. /** 
  187. * Splits variable name to chunks. 
  188. * @param string $variableName Fully qualified name of a variable. 
  189. * @throws \InvalidArgumentException if variable name is invalid. 
  190. * @return array 
  191. */ 
  192. private function _splitVariableName($variableName) 
  193. $bad_chars = preg_quote(self::NOT_VALID_NAME_CHARS, '/'); 
  194. $bad_seg_chars = preg_quote(self::NOT_VALID_SEGMENT_NAME_CHARS, '/'); 
  195.  
  196. $name_pattern = "(?:[^" . $bad_chars . "\s]+)|(?:\[[^" . $bad_seg_chars . "]+\])"; 
  197. $check_pattern = "/^((" . $name_pattern . ")\.)*(" . $name_pattern . ")\.?$/"; 
  198. $get_pattern = "/(?:" . $name_pattern . ")/"; 
  199.  
  200. if (!preg_match($check_pattern, $variableName)) { 
  201. throw new \InvalidArgumentException('variable name is invalid'); 
  202.  
  203. preg_match_all($get_pattern, $variableName, $matches); 
  204.  
  205. $chunks = array(); 
  206. foreach ($matches[0] as $chunk) { 
  207. // Remove wrapper braces if needed 
  208. if ($chunk[0] == '[') { 
  209. $chunk = substr($chunk, 1, -1); 
  210. $chunks[] = $chunk; 
  211.  
  212. return $chunks; 
  213.  
/vendor/xamin/handlebars.php/src/Handlebars/Context.php  
  1. class Context 
  2.  
  3. /** 
  4. * List of charcters that cannot be used in identifiers. 
  5. */ 
  6. const NOT_VALID_NAME_CHARS = '!"#%&\'()*+, ./;<=>@[\\]^`{|}~'; 
  7.  
  8. /** 
  9. * List of characters that cannot be used in identifiers in segment-literal 
  10. * notation. 
  11. */ 
  12. const NOT_VALID_SEGMENT_NAME_CHARS = "]"; 
  13.  
  14. /** 
  15. * @var array stack for context only top stack is available 
  16. */ 
  17. protected $stack = array(); 
  18.  
  19. /** 
  20. * @var array index stack for sections 
  21. */ 
  22. protected $index = array(); 
  23.  
  24. /** 
  25. * @var array key stack for objects 
  26. */ 
  27. protected $key = array(); 
  28.  
  29. /** 
  30. * @var array Special variables stack for sections. Each stack element can 
  31. * contain elements with "@index", "@key", "@first" and "@last" keys. 
  32. */ 
  33. protected $specialVariables = array(); 
  34.  
  35. /** 
  36. * Mustache rendering Context constructor. 
  37. * @param mixed $context Default rendering context (default: null) 
  38. */ 
  39. public function __construct($context = null) 
  40. if ($context !== null) { 
  41. $this->stack = array($context); 
  42.  
  43. /** 
  44. * Push a new Context frame onto the stack. 
  45. * @param mixed $value Object or array to use for context 
  46. * @return void 
  47. */ 
  48. public function push($value) 
  49. array_push($this->stack, $value); 
  50.  
  51. /** 
  52. * Push an array of special variables to stack. 
  53. * @param array $variables An associative array of special variables. 
  54. * @return void 
  55. * @see \Handlebars\Context::$specialVariables 
  56. */ 
  57. public function pushSpecialVariables($variables) 
  58. array_push($this->specialVariables, $variables); 
  59.  
  60. /** 
  61. * Pop the last Context frame from the stack. 
  62. * @return mixed Last Context frame (object or array) 
  63. */ 
  64. public function pop() 
  65. return array_pop($this->stack); 
  66.  
  67. /** 
  68. * Pop the last special variables set from the stack. 
  69. * @return array Associative array of special variables. 
  70. * @see \Handlebars\Context::$specialVariables 
  71. */ 
  72. public function popSpecialVariables() 
  73. return array_pop($this->specialVariables); 
  74.  
  75. /** 
  76. * Get the last Context frame. 
  77. * @return mixed Last Context frame (object or array) 
  78. */ 
  79. public function last() 
  80. return end($this->stack); 
  81.  
  82. /** 
  83. * Get the last special variables set from the stack. 
  84. * @return array Associative array of special variables. 
  85. * @see \Handlebars\Context::$specialVariables 
  86. */ 
  87. public function lastSpecialVariables() 
  88. return end($this->specialVariables); 
  89.  
  90. /** 
  91. * Change the current context to one of current context members 
  92. * @param string $variableName name of variable or a callable on current context 
  93. * @return mixed actual value 
  94. */ 
  95. public function with($variableName) 
  96. $value = $this->get($variableName); 
  97. $this->push($value); 
  98.  
  99. return $value; 
  100.  
  101. /** 
  102. * Get a available from current context 
  103. * Supported types : 
  104. * variable , ../variable , variable.variable , variable.[variable] , . 
  105. * @param string $variableName variable name to get from current context 
  106. * @param boolean $strict strict search? if not found then throw exception 
  107. * @throws \InvalidArgumentException in strict mode and variable not found 
  108. * @throws \RuntimeException if supplied argument is a malformed quoted string  
  109. * @throws \InvalidArgumentException if variable name is invalid 
  110. * @return mixed 
  111. */ 
  112. public function get($variableName, $strict = false) 
  113. if ($variableName instanceof \Handlebars\String) { 
  114. return (string)$variableName; 
  115. $variableName = trim($variableName); 
  116. $level = 0; 
  117. while (substr($variableName, 0, 3) == '../') { 
  118. $variableName = trim(substr($variableName, 3)); 
  119. $level++; 
  120. if (count($this->stack) < $level) { 
  121. if ($strict) { 
  122. throw new \InvalidArgumentException( 
  123. 'can not find variable in context' 
  124. ); 
  125.  
  126. return ''; 
  127. end($this->stack); 
  128. while ($level) { 
  129. prev($this->stack); 
  130. $level--; 
  131. $current = current($this->stack); 
  132. if (!$variableName) { 
  133. if ($strict) { 
  134. throw new \InvalidArgumentException( 
  135. 'can not find variable in context' 
  136. ); 
  137. return ''; 
  138. } elseif ($variableName == '.' || $variableName == 'this') { 
  139. return $current; 
  140. } elseif ($variableName[0] == '@') { 
  141. $specialVariables = $this->lastSpecialVariables(); 
  142. if (isset($specialVariables[$variableName])) { 
  143. return $specialVariables[$variableName]; 
  144. } elseif ($strict) { 
  145. throw new \InvalidArgumentException( 
  146. 'can not find variable in context' 
  147. ); 
  148. } else { 
  149. return ''; 
  150. } else { 
  151. $chunks = $this->_splitVariableName($variableName); 
  152. foreach ($chunks as $chunk) { 
  153. if (is_string($current) and $current == '') { 
  154. return $current; 
  155. $current = $this->_findVariableInContext($current, $chunk, $strict); 
  156. return $current; 
  157.  
  158. /** 
  159. * Check if $variable->$inside is available 
  160. * @param mixed $variable variable to check 
  161. * @param string $inside property/method to check 
  162. * @param boolean $strict strict search? if not found then throw exception 
  163. * @throws \InvalidArgumentException in strict mode and variable not found 
  164. * @return boolean true if exist 
  165. */ 
  166. private function _findVariableInContext($variable, $inside, $strict = false) 
  167. $value = null; 
  168. if (($inside !== '0' && empty($inside)) || ($inside == 'this')) { 
  169. return $variable; 
  170. } elseif (is_array($variable)) { 
  171. if (isset($variable[$inside]) || array_key_exists($inside, $variable)) { 
  172. return $variable[$inside]; 
  173. } elseif ($inside == "length") { 
  174. return count($variable); 
  175. } elseif (is_object($variable)) { 
  176. if (isset($variable->$inside)) { 
  177. return $variable->$inside; 
  178. } elseif (is_callable(array($variable, $inside))) { 
  179. return call_user_func(array($variable, $inside)); 
  180.  
  181. if ($strict) { 
  182. throw new \InvalidArgumentException('can not find variable in context'); 
  183.  
  184. return $value; 
  185.  
  186. /** 
  187. * Splits variable name to chunks. 
  188. * @param string $variableName Fully qualified name of a variable. 
  189. * @throws \InvalidArgumentException if variable name is invalid. 
  190. * @return array 
  191. */ 
  192. private function _splitVariableName($variableName) 
  193. $bad_chars = preg_quote(self::NOT_VALID_NAME_CHARS, '/'); 
  194. $bad_seg_chars = preg_quote(self::NOT_VALID_SEGMENT_NAME_CHARS, '/'); 
  195.  
  196. $name_pattern = "(?:[^" . $bad_chars . "\s]+)|(?:\[[^" . $bad_seg_chars . "]+\])"; 
  197. $check_pattern = "/^((" . $name_pattern . ")\.)*(" . $name_pattern . ")\.?$/"; 
  198. $get_pattern = "/(?:" . $name_pattern . ")/"; 
  199.  
  200. if (!preg_match($check_pattern, $variableName)) { 
  201. throw new \InvalidArgumentException('variable name is invalid'); 
  202.  
  203. preg_match_all($get_pattern, $variableName, $matches); 
  204.  
  205. $chunks = array(); 
  206. foreach ($matches[0] as $chunk) { 
  207. // Remove wrapper braces if needed 
  208. if ($chunk[0] == '[') { 
  209. $chunk = substr($chunk, 1, -1); 
  210. $chunks[] = $chunk; 
  211.  
  212. return $chunks; 
  213.