Collection.php 7.6 KB

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