Collection.php 8.5 KB

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