Collection.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Collection.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Support;
  20. use ArrayAccess;
  21. use ArrayIterator;
  22. use Countable;
  23. use IteratorAggregate;
  24. use JsonSerializable;
  25. use Serializable;
  26. /**
  27. * Class Collection.
  28. */
  29. class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
  30. {
  31. /**
  32. * The collection data.
  33. *
  34. * @var array
  35. */
  36. protected $items = [];
  37. /**
  38. * set data.
  39. *
  40. * @param mixed $items
  41. */
  42. public function __construct(array $items = [])
  43. {
  44. foreach ($items as $key => $value) {
  45. $this->set($key, $value);
  46. }
  47. }
  48. /**
  49. * Return all items.
  50. *
  51. * @return array
  52. */
  53. public function all()
  54. {
  55. return $this->items;
  56. }
  57. /**
  58. * Return specific items.
  59. *
  60. * @param array $keys
  61. *
  62. * @return array
  63. */
  64. public function only(array $keys)
  65. {
  66. $return = [];
  67. foreach ($keys as $key) {
  68. $value = $this->get($key);
  69. if (!is_null($value)) {
  70. $return[$key] = $value;
  71. }
  72. }
  73. return $return;
  74. }
  75. /**
  76. * Get all items except for those with the specified keys.
  77. *
  78. * @param mixed $keys
  79. *
  80. * @return static
  81. */
  82. public function except($keys)
  83. {
  84. $keys = is_array($keys) ? $keys : func_get_args();
  85. return new static(Arr::except($this->items, $keys));
  86. }
  87. /**
  88. * Merge data.
  89. *
  90. * @param Collection|array $items
  91. *
  92. * @return array
  93. */
  94. public function merge($items)
  95. {
  96. foreach ($items as $key => $value) {
  97. $this->set($key, $value);
  98. }
  99. return $this->all();
  100. }
  101. /**
  102. * To determine Whether the specified element exists.
  103. *
  104. * @param string $key
  105. *
  106. * @return bool
  107. */
  108. public function has($key)
  109. {
  110. return !is_null(Arr::get($this->items, $key));
  111. }
  112. /**
  113. * Retrieve the first item.
  114. *
  115. * @return mixed
  116. */
  117. public function first()
  118. {
  119. return reset($this->items);
  120. }
  121. /**
  122. * Retrieve the last item.
  123. *
  124. * @return bool
  125. */
  126. public function last()
  127. {
  128. $end = end($this->items);
  129. reset($this->items);
  130. return $end;
  131. }
  132. /**
  133. * add the item value.
  134. *
  135. * @param string $key
  136. * @param mixed $value
  137. */
  138. public function add($key, $value)
  139. {
  140. Arr::set($this->items, $key, $value);
  141. }
  142. /**
  143. * Set the item value.
  144. *
  145. * @param string $key
  146. * @param mixed $value
  147. */
  148. public function set($key, $value)
  149. {
  150. Arr::set($this->items, $key, $value);
  151. }
  152. /**
  153. * Retrieve item from Collection.
  154. *
  155. * @param string $key
  156. * @param mixed $default
  157. *
  158. * @return mixed
  159. */
  160. public function get($key, $default = null)
  161. {
  162. return Arr::get($this->items, $key, $default);
  163. }
  164. /**
  165. * Remove item form Collection.
  166. *
  167. * @param string $key
  168. */
  169. public function forget($key)
  170. {
  171. Arr::forget($this->items, $key);
  172. }
  173. /**
  174. * Build to array.
  175. *
  176. * @return array
  177. */
  178. public function toArray()
  179. {
  180. return $this->all();
  181. }
  182. /**
  183. * Build to json.
  184. *
  185. * @param int $option
  186. *
  187. * @return string
  188. */
  189. public function toJson($option = JSON_UNESCAPED_UNICODE)
  190. {
  191. return json_encode($this->all(), $option);
  192. }
  193. /**
  194. * To string.
  195. *
  196. * @return string
  197. */
  198. public function __toString()
  199. {
  200. return $this->toJson();
  201. }
  202. /**
  203. * (PHP 5 &gt;= 5.4.0)<br/>
  204. * Specify data which should be serialized to JSON.
  205. *
  206. * @see http://php.net/manual/en/jsonserializable.jsonserialize.php
  207. *
  208. * @return mixed data which can be serialized by <b>json_encode</b>,
  209. * which is a value of any type other than a resource
  210. */
  211. public function jsonSerialize()
  212. {
  213. return $this->items;
  214. }
  215. /**
  216. * (PHP 5 &gt;= 5.1.0)<br/>
  217. * String representation of object.
  218. *
  219. * @see http://php.net/manual/en/serializable.serialize.php
  220. *
  221. * @return string the string representation of the object or null
  222. */
  223. public function serialize()
  224. {
  225. return serialize($this->items);
  226. }
  227. /**
  228. * (PHP 5 &gt;= 5.0.0)<br/>
  229. * Retrieve an external iterator.
  230. *
  231. * @see http://php.net/manual/en/iteratoraggregate.getiterator.php
  232. *
  233. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  234. * <b>Traversable</b>
  235. */
  236. public function getIterator()
  237. {
  238. return new ArrayIterator($this->items);
  239. }
  240. /**
  241. * (PHP 5 &gt;= 5.1.0)<br/>
  242. * Count elements of an object.
  243. *
  244. * @see http://php.net/manual/en/countable.count.php
  245. *
  246. * @return int The custom count as an integer.
  247. * </p>
  248. * <p>
  249. * The return value is cast to an integer
  250. */
  251. public function count()
  252. {
  253. return count($this->items);
  254. }
  255. /**
  256. * (PHP 5 &gt;= 5.1.0)<br/>
  257. * Constructs the object.
  258. *
  259. * @see http://php.net/manual/en/serializable.unserialize.php
  260. *
  261. * @param string $serialized <p>
  262. * The string representation of the object.
  263. * </p>
  264. *
  265. * @return mixed|void
  266. */
  267. public function unserialize($serialized)
  268. {
  269. return $this->items = unserialize($serialized);
  270. }
  271. /**
  272. * Get a data by key.
  273. *
  274. * @param string $key
  275. *
  276. * @return mixed
  277. */
  278. public function __get($key)
  279. {
  280. return $this->get($key);
  281. }
  282. /**
  283. * Assigns a value to the specified data.
  284. *
  285. * @param string $key
  286. * @param mixed $value
  287. */
  288. public function __set($key, $value)
  289. {
  290. $this->set($key, $value);
  291. }
  292. /**
  293. * Whether or not an data exists by key.
  294. *
  295. * @param string $key
  296. *
  297. * @return bool
  298. */
  299. public function __isset($key)
  300. {
  301. return $this->has($key);
  302. }
  303. /**
  304. * Unsets an data by key.
  305. *
  306. * @param string $key
  307. */
  308. public function __unset($key)
  309. {
  310. $this->forget($key);
  311. }
  312. /**
  313. * var_export.
  314. *
  315. * @return array
  316. */
  317. public function __set_state()
  318. {
  319. return $this->all();
  320. }
  321. /**
  322. * (PHP 5 &gt;= 5.0.0)<br/>
  323. * Whether a offset exists.
  324. *
  325. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  326. *
  327. * @param mixed $offset <p>
  328. * An offset to check for.
  329. * </p>
  330. *
  331. * @return bool true on success or false on failure.
  332. * The return value will be casted to boolean if non-boolean was returned
  333. */
  334. public function offsetExists($offset)
  335. {
  336. return $this->has($offset);
  337. }
  338. /**
  339. * (PHP 5 &gt;= 5.0.0)<br/>
  340. * Offset to unset.
  341. *
  342. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  343. *
  344. * @param mixed $offset <p>
  345. * The offset to unset.
  346. * </p>
  347. */
  348. public function offsetUnset($offset)
  349. {
  350. if ($this->offsetExists($offset)) {
  351. $this->forget($offset);
  352. }
  353. }
  354. /**
  355. * (PHP 5 &gt;= 5.0.0)<br/>
  356. * Offset to retrieve.
  357. *
  358. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  359. *
  360. * @param mixed $offset <p>
  361. * The offset to retrieve.
  362. * </p>
  363. *
  364. * @return mixed Can return all value types
  365. */
  366. public function offsetGet($offset)
  367. {
  368. return $this->offsetExists($offset) ? $this->get($offset) : null;
  369. }
  370. /**
  371. * (PHP 5 &gt;= 5.0.0)<br/>
  372. * Offset to set.
  373. *
  374. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  375. *
  376. * @param mixed $offset <p>
  377. * The offset to assign the value to.
  378. * </p>
  379. * @param mixed $value <p>
  380. * The value to set.
  381. * </p>
  382. */
  383. public function offsetSet($offset, $value)
  384. {
  385. $this->set($offset, $value);
  386. }
  387. }