Set.php (6015B)
1 <?php 2 /** 3 * Slim - a micro PHP 5 framework 4 * 5 * @author Josh Lockhart <info@slimframework.com> 6 * @copyright 2011 Josh Lockhart 7 * @link http://www.slimframework.com 8 * @license http://www.slimframework.com/license 9 * @version 2.4.2 10 * @package Slim 11 * 12 * MIT LICENSE 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining 15 * a copy of this software and associated documentation files (the 16 * "Software"), to deal in the Software without restriction, including 17 * without limitation the rights to use, copy, modify, merge, publish, 18 * distribute, sublicense, and/or sell copies of the Software, and to 19 * permit persons to whom the Software is furnished to do so, subject to 20 * the following conditions: 21 * 22 * The above copyright notice and this permission notice shall be 23 * included in all copies or substantial portions of the Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 29 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 30 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 31 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 32 */ 33 namespace Slim\Helper; 34 35 class Set implements \ArrayAccess, \Countable, \IteratorAggregate 36 { 37 /** 38 * Key-value array of arbitrary data 39 * @var array 40 */ 41 protected $data = array(); 42 43 /** 44 * Constructor 45 * @param array $items Pre-populate set with this key-value array 46 */ 47 public function __construct($items = array()) 48 { 49 $this->replace($items); 50 } 51 52 /** 53 * Normalize data key 54 * 55 * Used to transform data key into the necessary 56 * key format for this set. Used in subclasses 57 * like \Slim\Http\Headers. 58 * 59 * @param string $key The data key 60 * @return mixed The transformed/normalized data key 61 */ 62 protected function normalizeKey($key) 63 { 64 return $key; 65 } 66 67 /** 68 * Set data key to value 69 * @param string $key The data key 70 * @param mixed $value The data value 71 */ 72 public function set($key, $value) 73 { 74 $this->data[$this->normalizeKey($key)] = $value; 75 } 76 77 /** 78 * Get data value with key 79 * @param string $key The data key 80 * @param mixed $default The value to return if data key does not exist 81 * @return mixed The data value, or the default value 82 */ 83 public function get($key, $default = null) 84 { 85 if ($this->has($key)) { 86 $isInvokable = is_object($this->data[$this->normalizeKey($key)]) && method_exists($this->data[$this->normalizeKey($key)], '__invoke'); 87 88 return $isInvokable ? $this->data[$this->normalizeKey($key)]($this) : $this->data[$this->normalizeKey($key)]; 89 } 90 91 return $default; 92 } 93 94 /** 95 * Add data to set 96 * @param array $items Key-value array of data to append to this set 97 */ 98 public function replace($items) 99 { 100 foreach ($items as $key => $value) { 101 $this->set($key, $value); // Ensure keys are normalized 102 } 103 } 104 105 /** 106 * Fetch set data 107 * @return array This set's key-value data array 108 */ 109 public function all() 110 { 111 return $this->data; 112 } 113 114 /** 115 * Fetch set data keys 116 * @return array This set's key-value data array keys 117 */ 118 public function keys() 119 { 120 return array_keys($this->data); 121 } 122 123 /** 124 * Does this set contain a key? 125 * @param string $key The data key 126 * @return boolean 127 */ 128 public function has($key) 129 { 130 return array_key_exists($this->normalizeKey($key), $this->data); 131 } 132 133 /** 134 * Remove value with key from this set 135 * @param string $key The data key 136 */ 137 public function remove($key) 138 { 139 unset($this->data[$this->normalizeKey($key)]); 140 } 141 142 /** 143 * Property Overloading 144 */ 145 146 public function __get($key) 147 { 148 return $this->get($key); 149 } 150 151 public function __set($key, $value) 152 { 153 $this->set($key, $value); 154 } 155 156 public function __isset($key) 157 { 158 return $this->has($key); 159 } 160 161 public function __unset($key) 162 { 163 return $this->remove($key); 164 } 165 166 /** 167 * Clear all values 168 */ 169 public function clear() 170 { 171 $this->data = array(); 172 } 173 174 /** 175 * Array Access 176 */ 177 178 public function offsetExists($offset) 179 { 180 return $this->has($offset); 181 } 182 183 public function offsetGet($offset) 184 { 185 return $this->get($offset); 186 } 187 188 public function offsetSet($offset, $value) 189 { 190 $this->set($offset, $value); 191 } 192 193 public function offsetUnset($offset) 194 { 195 $this->remove($offset); 196 } 197 198 /** 199 * Countable 200 */ 201 202 public function count() 203 { 204 return count($this->data); 205 } 206 207 /** 208 * IteratorAggregate 209 */ 210 211 public function getIterator() 212 { 213 return new \ArrayIterator($this->data); 214 } 215 216 /** 217 * Ensure a value or object will remain globally unique 218 * @param string $key The value or object name 219 * @param Closure The closure that defines the object 220 * @return mixed 221 */ 222 public function singleton($key, $value) 223 { 224 $this->set($key, function ($c) use ($value) { 225 static $object; 226 227 if (null === $object) { 228 $object = $value($c); 229 } 230 231 return $object; 232 }); 233 } 234 235 /** 236 * Protect closure from being directly invoked 237 * @param Closure $callable A closure to keep from being invoked and evaluated 238 * @return Closure 239 */ 240 public function protect(\Closure $callable) 241 { 242 return function () use ($callable) { 243 return $callable; 244 }; 245 } 246 }