Collection.php 9.1 KB

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