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