/sabel/util/Map.php

https://github.com/reoring/sabel · PHP · 293 lines · 231 code · 53 blank · 9 comment · 19 complexity · e1ed5c1bf0d84d10a8932866b57c411c MD5 · raw file

  1. <?php
  2. /**
  3. * Sabel_Util_Map
  4. *
  5. * @category Util
  6. * @package org.sabel.util
  7. * @author Ebine Yutaka <ebine.yutaka@sabel.jp>
  8. * @copyright 2004-2008 Mori Reo <mori.reo@sabel.jp>
  9. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  10. */
  11. class Sabel_Util_Map extends Sabel_Object implements Iterator
  12. {
  13. protected $array = array();
  14. protected $count = 0;
  15. protected $index = 0;
  16. protected $key = null;
  17. protected $value = null;
  18. public function __construct($array = null)
  19. {
  20. if ($array !== null) {
  21. if (is_array($array)) {
  22. $this->array = $array;
  23. $this->count = count($array);
  24. } else {
  25. $message = __METHOD__ . "() argument must be an array.";
  26. throw new Sabel_Exception_InvalidArgument($message);
  27. }
  28. }
  29. }
  30. public function set($array)
  31. {
  32. $this->array = $array;
  33. $this->count = count($array);
  34. }
  35. public function isEmpty()
  36. {
  37. return empty($this->array);
  38. }
  39. public function toArray()
  40. {
  41. return $this->array;
  42. }
  43. public function clear()
  44. {
  45. $array = $this->array;
  46. $this->array = array();
  47. $this->count = 0;
  48. return $array;
  49. }
  50. public function count()
  51. {
  52. return $this->count;
  53. }
  54. public function put($key, $value)
  55. {
  56. $key = $this->convertToString($key);
  57. $this->array[$key] = $value;
  58. $this->count++;
  59. return $this;
  60. }
  61. public function push($value)
  62. {
  63. $this->array[] = $value;
  64. $this->count++;
  65. return $this;
  66. }
  67. public function get($key)
  68. {
  69. $key = $this->convertToString($key);
  70. if (isset($this->array[$key])) {
  71. return $this->toObject($this->array[$key]);
  72. } else {
  73. return null;
  74. }
  75. }
  76. public function has($key)
  77. {
  78. $key = $this->convertToString($key);
  79. return isset($this->array[$key]);
  80. }
  81. public function exists($key)
  82. {
  83. $key = $this->convertToString($key);
  84. return array_key_exists($key, $this->array);
  85. }
  86. public function keys()
  87. {
  88. return array_keys($this->array);
  89. }
  90. public function values()
  91. {
  92. $values = array();
  93. foreach ($this->array as $value) {
  94. $values[] = $this->toObject($value);
  95. }
  96. return $values;
  97. }
  98. public function remove($key)
  99. {
  100. $key = $this->convertToString($key);
  101. if ($this->has($key)) {
  102. $value = $this->get($key);
  103. unset($this->array[$key]);
  104. $this->count--;
  105. return $value;
  106. }
  107. }
  108. public function pop()
  109. {
  110. if ($this->count === 0) {
  111. return null;
  112. } else {
  113. $value = array_pop($this->array);
  114. $this->count--;
  115. return $this->toObject($value);
  116. }
  117. }
  118. public function shift()
  119. {
  120. if ($this->count === 0) {
  121. return null;
  122. } else {
  123. $value = array_shift($this->array);
  124. $this->count--;
  125. return $this->toObject($value);
  126. }
  127. }
  128. public function sort()
  129. {
  130. sort($this->array);
  131. return $this;
  132. }
  133. public function rsort()
  134. {
  135. rsort($this->array);
  136. return $this;
  137. }
  138. public function reverse()
  139. {
  140. $this->array = array_reverse($this->array);
  141. return $this;
  142. }
  143. public function unique()
  144. {
  145. $this->array = array_unique($this->array);
  146. $this->count = count($this->array);
  147. return $this;
  148. }
  149. public function implode($glue = ", ")
  150. {
  151. return new Sabel_Util_String(implode($glue, $this->array));
  152. }
  153. public function sum()
  154. {
  155. return array_sum($this->array);
  156. }
  157. public function hasMoreElements()
  158. {
  159. return $this->valid();
  160. }
  161. public function nextElement()
  162. {
  163. $value = $this->current();
  164. $this->next();
  165. return $value;
  166. }
  167. public function search($value, $strict = false)
  168. {
  169. $key = array_search($value, $this->array, $strict);
  170. if ($key !== false) {
  171. $key = new Sabel_Util_String($key);
  172. }
  173. return $key;
  174. }
  175. public function merge($array)
  176. {
  177. $tarray =& $this->array;
  178. $count =& $this->count;
  179. foreach ($array as $key => $val) {
  180. $tarray[$key] = $val;
  181. $count++;
  182. }
  183. return $this;
  184. }
  185. public function current()
  186. {
  187. return $this->value;
  188. }
  189. public function key()
  190. {
  191. return $this->key;
  192. }
  193. public function valid()
  194. {
  195. if ($this->index < $this->count) {
  196. list ($this->key, $value) = each($this->array);
  197. $this->value = $this->toObject($value);
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. public function next()
  204. {
  205. $this->index++;
  206. }
  207. public function rewind()
  208. {
  209. $this->reset();
  210. $this->index = 0;
  211. $this->count = count($this->array);
  212. }
  213. public function reset()
  214. {
  215. reset($this->array);
  216. }
  217. protected function convertToString($value)
  218. {
  219. if (is_string($value) || is_int($value)) {
  220. return $value;
  221. } elseif ($value instanceof Sabel_Object) {
  222. return $value->toString();
  223. } elseif (is_object($value) && method_exists($value, "__toString")) {
  224. return $value->__toString();
  225. } else {
  226. $message = __METHOD__ . "() cannot convert the given argument into the string.";
  227. throw new Sabel_Exception_Runtime($message);
  228. }
  229. }
  230. protected function toObject($value)
  231. {
  232. if (is_string($value)) {
  233. return new Sabel_Util_String($value);
  234. } elseif (is_array($value)) {
  235. return new Sabel_Util_Map($value);
  236. } else {
  237. return $value;
  238. }
  239. }
  240. }