PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/xamin/handlebars.php/src/Handlebars/Helpers.php

https://gitlab.com/fabiorf/chat
PHP | 285 lines | 216 code | 8 blank | 61 comment | 7 complexity | 75e30e348edf107a30fda83cf0b403f6 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of Handlebars-php
  4. * Base on mustache-php https://github.com/bobthecow/mustache.php
  5. *
  6. * PHP version 5.3
  7. *
  8. * @category Xamin
  9. * @package Handlebars
  10. * @author fzerorubigd <fzerorubigd@gmail.com>
  11. * @author Behrooz Shabani <everplays@gmail.com>
  12. * @author Dmitriy Simushev <simushevds@gmail.com>
  13. * @author Jeff Turcotte <jeff.turcotte@gmail.com>
  14. * @copyright 2012 (c) ParsPooyesh Co
  15. * @copyright 2013 (c) Behrooz Shabani
  16. * @license MIT <http://opensource.org/licenses/MIT>
  17. * @version GIT: $Id$
  18. * @link http://xamin.ir
  19. */
  20. namespace Handlebars;
  21. /**
  22. * Handlebars helpers
  23. *
  24. * a collection of helper function. normally a function like
  25. * function ($sender, $name, $arguments) $arguments is unscaped arguments and
  26. * is a string, not array
  27. *
  28. * @category Xamin
  29. * @package Handlebars
  30. * @author fzerorubigd <fzerorubigd@gmail.com>
  31. * @copyright 2012 (c) ParsPooyesh Co
  32. * @license MIT <http://opensource.org/licenses/MIT>
  33. * @version Release: @package_version@
  34. * @link http://xamin.ir
  35. */
  36. class Helpers
  37. {
  38. /**
  39. * @var array array of helpers
  40. */
  41. protected $helpers = array();
  42. /**
  43. * Create new helper container class
  44. *
  45. * @param array $helpers array of name=>$value helpers
  46. * @param array|bool $defaults add defaults helper
  47. * (if, unless, each,with, bindAttr)
  48. *
  49. * @throws \InvalidArgumentException when $helpers is not an array
  50. * (or traversable) or helper is not a callable
  51. */
  52. public function __construct($helpers = null, $defaults = true)
  53. {
  54. if ($defaults) {
  55. $this->addDefaultHelpers();
  56. }
  57. if ($helpers != null) {
  58. if (!is_array($helpers) && !$helpers instanceof \Traversable) {
  59. throw new \InvalidArgumentException(
  60. 'HelperCollection constructor expects an array of helpers'
  61. );
  62. }
  63. foreach ($helpers as $name => $helper) {
  64. $this->add($name, $helper);
  65. }
  66. }
  67. }
  68. /**
  69. * Add default helpers (if unless each with bindAttr)
  70. *
  71. * @return void
  72. */
  73. protected function addDefaultHelpers()
  74. {
  75. $this->add('if', new Helper\IfHelper());
  76. $this->add('each', new Helper\EachHelper());
  77. $this->add('unless', new Helper\UnlessHelper());
  78. $this->add('with', new Helper\WithHelper());
  79. //Just for compatibility with ember
  80. $this->add('bindAttr', new Helper\BindAttrHelper());
  81. }
  82. /**
  83. * Add a new helper to helpers
  84. *
  85. * @param string $name helper name
  86. * @param mixed $helper a callable or Helper implementation as a helper
  87. *
  88. * @throws \InvalidArgumentException if $helper is not a callable
  89. * @return void
  90. */
  91. public function add($name, $helper)
  92. {
  93. if (!is_callable($helper) && ! $helper instanceof Helper) {
  94. throw new \InvalidArgumentException(
  95. sprintf(
  96. "%s Helper is not a callable or doesn't implement the Helper interface.",
  97. $name
  98. )
  99. );
  100. }
  101. $this->helpers[$name] = $helper;
  102. }
  103. /**
  104. * Add all helpers from the specified collection to the current one.
  105. *
  106. * The method will override helpers from the current collections with same
  107. * named helpers from the specified collection.
  108. *
  109. * @param Helpers $helpers A collection which helpers should be added.
  110. *
  111. * @return void
  112. */
  113. public function addHelpers(Helpers $helpers)
  114. {
  115. $this->helpers = $helpers->getAll() + $this->helpers;
  116. }
  117. /**
  118. * Calls a helper, whether it be a Closure or Helper instance
  119. *
  120. * @param string $name The name of the helper
  121. * @param \Handlebars\Template $template The template instance
  122. * @param \Handlebars\Context $context The current context
  123. * @param array $args The arguments passed the the helper
  124. * @param string $source The source
  125. *
  126. * @throws \InvalidArgumentException
  127. * @return mixed The helper return value
  128. */
  129. public function call($name, Template $template, Context $context, $args, $source)
  130. {
  131. if (!$this->has($name)) {
  132. throw new \InvalidArgumentException(
  133. sprintf(
  134. 'Unknown helper: "%s"',
  135. $name
  136. )
  137. );
  138. }
  139. if ($this->helpers[$name] instanceof Helper) {
  140. return $this->helpers[$name]->execute(
  141. $template, $context, $args, $source
  142. );
  143. }
  144. return call_user_func($this->helpers[$name], $template, $context, $args, $source);
  145. }
  146. /**
  147. * Check if $name helper is available
  148. *
  149. * @param string $name helper name
  150. *
  151. * @return boolean
  152. */
  153. public function has($name)
  154. {
  155. return array_key_exists($name, $this->helpers);
  156. }
  157. /**
  158. * Get a helper. __magic__ method :)
  159. *
  160. * @param string $name helper name
  161. *
  162. * @throws \InvalidArgumentException if $name is not available
  163. * @return callable helper function
  164. */
  165. public function __get($name)
  166. {
  167. if (!$this->has($name)) {
  168. throw new \InvalidArgumentException(
  169. sprintf(
  170. 'Unknown helper: "%s"',
  171. $name
  172. )
  173. );
  174. }
  175. return $this->helpers[$name];
  176. }
  177. /**
  178. * Check if $name helper is available __magic__ method :)
  179. *
  180. * @param string $name helper name
  181. *
  182. * @return boolean
  183. * @see Handlebras_Helpers::has
  184. */
  185. public function __isset($name)
  186. {
  187. return $this->has($name);
  188. }
  189. /**
  190. * Add a new helper to helpers __magic__ method :)
  191. *
  192. * @param string $name helper name
  193. * @param callable $helper a function as a helper
  194. *
  195. * @return void
  196. */
  197. public function __set($name, $helper)
  198. {
  199. $this->add($name, $helper);
  200. }
  201. /**
  202. * Unset a helper
  203. *
  204. * @param string $name helper name to remove
  205. *
  206. * @return void
  207. */
  208. public function __unset($name)
  209. {
  210. $this->remove($name);
  211. }
  212. /**
  213. * Check whether a given helper is present in the collection.
  214. *
  215. * @param string $name helper name
  216. *
  217. * @throws \InvalidArgumentException if the requested helper is not present.
  218. * @return void
  219. */
  220. public function remove($name)
  221. {
  222. if (!$this->has($name)) {
  223. throw new \InvalidArgumentException(
  224. sprintf(
  225. 'Unknown helper: "%s"',
  226. $name
  227. )
  228. );
  229. }
  230. unset($this->helpers[$name]);
  231. }
  232. /**
  233. * Clear the helper collection.
  234. *
  235. * Removes all helpers from this collection
  236. *
  237. * @return void
  238. */
  239. public function clear()
  240. {
  241. $this->helpers = array();
  242. }
  243. /**
  244. * Check whether the helper collection is empty.
  245. *
  246. * @return boolean True if the collection is empty
  247. */
  248. public function isEmpty()
  249. {
  250. return empty($this->helpers);
  251. }
  252. /**
  253. * Returns all helpers from the collection.
  254. *
  255. * @return array Associative array of helpers which keys are helpers names
  256. * and the values are the helpers.
  257. */
  258. public function getAll()
  259. {
  260. return $this->helpers;
  261. }
  262. }