PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/class/base/HashMap.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 386 lines | 156 code | 40 blank | 190 comment | 12 complexity | 7d5faa06f2205fda862b4a2843f065cb MD5 | raw file
  1. <?php
  2. /**
  3. * 連想配列クラス
  4. *
  5. * キー、値ともに型は限定しない
  6. *
  7. * PHP version 5
  8. *
  9. * @package class.base
  10. * @author CharcoalPHP Development Team
  11. * @copyright 2008 stk2k, sazysoft
  12. */
  13. class Charcoal_HashMap extends Charcoal_Collection implements ArrayAccess, IteratorAggregate
  14. {
  15. /**
  16. * Retrieve default value
  17. *
  18. * @return Charcoal_HashMap default value
  19. */
  20. public static function defaultValue()
  21. {
  22. return new self();
  23. }
  24. /**
  25. * get key list
  26. */
  27. public function getKeys() {
  28. return array_keys( $this->values );
  29. }
  30. /**
  31. * check if specified key is in the list
  32. *
  33. * @param mixed $key
  34. *
  35. * @return bool
  36. */
  37. public function keyExists( $key )
  38. {
  39. return array_key_exists( $key, $this->values );
  40. }
  41. /**
  42. * Check if the collection is empty
  43. *
  44. * @return bool TRUE if this collection has no elements, FALSE otherwise
  45. */
  46. public function isEmpty()
  47. {
  48. return count( $this->values ) === 0;
  49. }
  50. /**
  51. * Get an element value
  52. *
  53. * @param mixed $key
  54. *
  55. * @return mixed|NULL
  56. */
  57. public function get( $key )
  58. {
  59. return $this->offsetGet( $key );
  60. }
  61. /**
  62. * Get all values with keys
  63. *
  64. * @return array
  65. */
  66. public function getAll()
  67. {
  68. return $this->values;
  69. }
  70. /**
  71. * update an element value
  72. *
  73. * @param mixed $key
  74. * @param mixed $value
  75. */
  76. public function set( $key, $value )
  77. {
  78. $this->offsetSet( $key, $value );
  79. }
  80. /**
  81. * Get an element value
  82. *
  83. * @param mixed $key
  84. *
  85. * @return mixed|NULL
  86. */
  87. public function __get( $key )
  88. {
  89. return $this->offsetGet( $key );
  90. }
  91. /**
  92. * Set an element value
  93. *
  94. * @param mixed $key
  95. * @param mixed $value
  96. */
  97. public function __set( $key, $value )
  98. {
  99. $this->offsetSet( $key, $value );
  100. }
  101. /**
  102. * ArrayAccess interface : offsetGet() implementation
  103. *
  104. * @param mixed $offset
  105. *
  106. * @return mixed|NULL
  107. */
  108. public function offsetGet($offset)
  109. {
  110. if ( is_object($offset) ){
  111. $offset = $offset->__toString();
  112. }
  113. return isset($this->values[ $offset ]) ? $this->values[ $offset ] : NULL;
  114. }
  115. /**
  116. * ArrayAccess interface : offsetSet() implementation
  117. *
  118. * @param mixed $offset
  119. * @param mixed $value
  120. */
  121. public function offsetSet($offset, $value)
  122. {
  123. if ( is_object($offset) ){
  124. $offset = $offset->__toString();
  125. }
  126. $this->values[ $offset ] = $value;
  127. }
  128. /**
  129. * ArrayAccess interface : offsetExists() implementation
  130. *
  131. * @param mixed $offset
  132. *
  133. * @return bool
  134. */
  135. public function offsetExists($offset)
  136. {
  137. return isset($this->values[$offset]);
  138. }
  139. /**
  140. * ArrayAccess interface : offsetUnset() implementation
  141. *
  142. * @param mixed $offset
  143. */
  144. public function offsetUnset($offset)
  145. {
  146. unset($this->values[$offset]);
  147. }
  148. /**
  149. * Countable interface: count() implementation
  150. */
  151. public function count()
  152. {
  153. return count( $this->values );
  154. }
  155. /**
  156. * Set all array elements
  157. *
  158. * @param array|Charcoal_Vector $array array data to set
  159. */
  160. public function setArray( $array )
  161. {
  162. $this->values = $array;
  163. if ($array instanceof Charcoal_Vector){
  164. $this->values = $array->toArray();
  165. }
  166. else if ( is_array($array) ){
  167. $this->values = $array;
  168. }
  169. else{
  170. _throw( new Charcoal_InvalidArgumentException($array) );
  171. }
  172. }
  173. /**
  174. * Set all hashmap elements
  175. *
  176. * @param array|Charcoal_HashMap $map hashmap data to set
  177. */
  178. public function setHashMap( $map )
  179. {
  180. if ($map instanceof Charcoal_HashMap){
  181. $this->values = $map->toArray();
  182. }
  183. else if ( is_array($map) ){
  184. $this->values = $map;
  185. }
  186. else{
  187. _throw( new Charcoal_InvalidArgumentException($map) );
  188. }
  189. }
  190. /**
  191. * Merge with array
  192. *
  193. * @param array $array array data to merge
  194. * @param boolean $overwrite TRUE means overwrite if the original element exists
  195. */
  196. public function mergeArray( $array, $overwrite = TRUE )
  197. {
  198. // Charcoal_ParamTrait::validatRawArray( 1, $array );
  199. // Charcoal_ParamTrait::validateBoolean( 2, $overwrite );
  200. $overwrite = ub($overwrite);
  201. if ( $overwrite ){
  202. foreach( $array as $key => $value ){
  203. $this->offsetSet( $key, $value );
  204. }
  205. }
  206. }
  207. /**
  208. * Merge with hashmap
  209. *
  210. * @param array $array hash map data to merge
  211. * @param boolean $overwrite TRUE means overwrite if the original element exists
  212. */
  213. public function mergeHashMap( $map, $overwrite = TRUE )
  214. {
  215. // Charcoal_ParamTrait::validateHashMap( 1, $map );
  216. // Charcoal_ParamTrait::validateBoolean( 2, $overwrite );
  217. $overwrite = ub($overwrite);
  218. if ( $overwrite ){
  219. foreach( $map as $key => $value ){
  220. $this->offsetSet( $key, $value );
  221. }
  222. }
  223. }
  224. /**
  225. * Get element value as string
  226. *
  227. * @param string $key Key string to get
  228. * @param string $default_value default value
  229. * @param string $encoding charcter encoding
  230. *
  231. * @return Charcoal_String
  232. */
  233. public function getString( $key, $default_value = NULL, $encoding = NULL )
  234. {
  235. // Charcoal_ParamTrait::validateString( 1, $key );
  236. // Charcoal_ParamTrait::validateString( 2, $default_value, TRUE );
  237. return Charcoal_ArrayTrait::getString( $this->values, $key, $default_value, $encoding );
  238. }
  239. /**
  240. * Get element value as array
  241. *
  242. * @param string $key Key string to get
  243. * @param array $default_value default value
  244. *
  245. * @return Charcoal_Vector
  246. */
  247. public function getArray( $key, $default_value = NULL )
  248. {
  249. // Charcoal_ParamTrait::validateString( 1, $key );
  250. // Charcoal_ParamTrait::validateVector( 2, $default_value, TRUE );
  251. return Charcoal_ArrayTrait::getArray( $this->values, $key, $default_value );
  252. }
  253. /**
  254. * Get element value as associative array
  255. *
  256. * @param string $key Key string to get
  257. * @param array $default_value default value
  258. *
  259. * @return Charcoal_HashMap
  260. */
  261. public function getHashMap( $key, $default_value = NULL )
  262. {
  263. // Charcoal_ParamTrait::validateString( 1, $key );
  264. // Charcoal_ParamTrait::validateHashMap( 2, $default_value, TRUE );
  265. return Charcoal_ArrayTrait::getHashMap( $this->values, $key, $default_value );
  266. }
  267. /**
  268. * Get element value as boolean
  269. *
  270. * @param string $key Key string to get
  271. * @param bool $default_value default value
  272. *
  273. * @return Charcoal_Boolean
  274. */
  275. public function getBoolean( $key, $default_value = NULL )
  276. {
  277. // Charcoal_ParamTrait::validateString( 1, $key );
  278. // Charcoal_ParamTrait::validateBoolean( 2, $default_value, TRUE );
  279. return Charcoal_ArrayTrait::getBoolean( $this->values, $key, $default_value );
  280. }
  281. /**
  282. * Get element value as integer
  283. *
  284. * @param string $key Key string to get
  285. * @param int $default_value default value
  286. *
  287. * @return Charcoal_Integer
  288. */
  289. public function getInteger( $key, $default_value = NULL )
  290. {
  291. // Charcoal_ParamTrait::validateString( 1, $key );
  292. // Charcoal_ParamTrait::validateInteger( 2, $default_value, TRUE );
  293. return Charcoal_ArrayTrait::getInteger( $this->values, $key, $default_value );
  294. }
  295. /**
  296. * Get element value as float
  297. *
  298. * @param string $key Key string to get
  299. * @param float $default_value default value
  300. *
  301. * @return Charcoal_Float
  302. */
  303. public function getFloat( $key, $default_value = NULL )
  304. {
  305. // Charcoal_ParamTrait::validateString( 1, $key );
  306. // Charcoal_ParamTrait::validatFloat( 2, $default_value, TRUE );
  307. return Charcoal_ArrayTrait::getFloat( $this->values, $key, $default_value );
  308. }
  309. /**
  310. * convert to array
  311. *
  312. * @return array
  313. */
  314. public function toArray()
  315. {
  316. if ( is_array($this->values) ){
  317. return $this->values;
  318. }
  319. return array_diff( $this->values, array() );
  320. }
  321. /**
  322. * make string glued by a delimiter
  323. */
  324. public function implodeAssoc( $glue = ',' )
  325. {
  326. return Charcoal_System::implodeAssoc( $glue, $this->values );
  327. }
  328. /**
  329. * String expression of this object
  330. *
  331. * @return string
  332. */
  333. public function toString()
  334. {
  335. $b = '';
  336. foreach ($this->values as $key => $value) {
  337. if ( !empty($b) ){
  338. $b .= '/';
  339. }
  340. $key = Charcoal_System::toString($key);
  341. $value = Charcoal_System::toString($value);
  342. $b .= "$key=$value";
  343. }
  344. return "[$b]";
  345. }
  346. }