HelperCollection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A collection of helpers for a Mustache instance.
  12. */
  13. class Mustache_HelperCollection
  14. {
  15. private $helpers = array();
  16. /**
  17. * Helper Collection constructor.
  18. *
  19. * Optionally accepts an array (or Traversable) of `$name => $helper` pairs.
  20. *
  21. * @throws Mustache_Exception_InvalidArgumentException if the $helpers argument isn't an array or Traversable
  22. *
  23. * @param array|Traversable $helpers (default: null)
  24. */
  25. public function __construct($helpers = null)
  26. {
  27. if ($helpers !== null) {
  28. if (!is_array($helpers) && !$helpers instanceof Traversable) {
  29. throw new Mustache_Exception_InvalidArgumentException('HelperCollection constructor expects an array of helpers');
  30. }
  31. foreach ($helpers as $name => $helper) {
  32. $this->add($name, $helper);
  33. }
  34. }
  35. }
  36. /**
  37. * Magic mutator.
  38. *
  39. * @see Mustache_HelperCollection::add
  40. *
  41. * @param string $name
  42. * @param mixed $helper
  43. */
  44. public function __set($name, $helper)
  45. {
  46. $this->add($name, $helper);
  47. }
  48. /**
  49. * Add a helper to this collection.
  50. *
  51. * @param string $name
  52. * @param mixed $helper
  53. */
  54. public function add($name, $helper)
  55. {
  56. $this->helpers[$name] = $helper;
  57. }
  58. /**
  59. * Magic accessor.
  60. *
  61. * @see Mustache_HelperCollection::get
  62. *
  63. * @param string $name
  64. *
  65. * @return mixed Helper
  66. */
  67. public function __get($name)
  68. {
  69. return $this->get($name);
  70. }
  71. /**
  72. * Get a helper by name.
  73. *
  74. * @throws Mustache_Exception_UnknownHelperException If helper does not exist.
  75. *
  76. * @param string $name
  77. *
  78. * @return mixed Helper
  79. */
  80. public function get($name)
  81. {
  82. if (!$this->has($name)) {
  83. throw new Mustache_Exception_UnknownHelperException($name);
  84. }
  85. return $this->helpers[$name];
  86. }
  87. /**
  88. * Magic isset().
  89. *
  90. * @see Mustache_HelperCollection::has
  91. *
  92. * @param string $name
  93. *
  94. * @return boolean True if helper is present
  95. */
  96. public function __isset($name)
  97. {
  98. return $this->has($name);
  99. }
  100. /**
  101. * Check whether a given helper is present in the collection.
  102. *
  103. * @param string $name
  104. *
  105. * @return boolean True if helper is present
  106. */
  107. public function has($name)
  108. {
  109. return array_key_exists($name, $this->helpers);
  110. }
  111. /**
  112. * Magic unset().
  113. *
  114. * @see Mustache_HelperCollection::remove
  115. *
  116. * @param string $name
  117. */
  118. public function __unset($name)
  119. {
  120. $this->remove($name);
  121. }
  122. /**
  123. * Check whether a given helper is present in the collection.
  124. *
  125. * @throws Mustache_Exception_UnknownHelperException if the requested helper is not present.
  126. *
  127. * @param string $name
  128. */
  129. public function remove($name)
  130. {
  131. if (!$this->has($name)) {
  132. throw new Mustache_Exception_UnknownHelperException($name);
  133. }
  134. unset($this->helpers[$name]);
  135. }
  136. /**
  137. * Clear the helper collection.
  138. *
  139. * Removes all helpers from this collection
  140. */
  141. public function clear()
  142. {
  143. $this->helpers = array();
  144. }
  145. /**
  146. * Check whether the helper collection is empty.
  147. *
  148. * @return boolean True if the collection is empty
  149. */
  150. public function isEmpty()
  151. {
  152. return empty($this->helpers);
  153. }
  154. }