Collection.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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|null $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. #[\ReturnTypeWillChange]
  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>ArrayIterator</b>
  224. */
  225. public function getIterator(): \Traversable
  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(): int
  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 $data <p>
  251. * The string representation of the object.
  252. * </p>
  253. *
  254. * @return mixed|void
  255. */
  256. public function unserialize($data)
  257. {
  258. return $this->items = unserialize($data);
  259. }
  260. /**
  261. * (PHP 5 &gt;= 5.0.0)<br/>
  262. * Whether a offset exists.
  263. *
  264. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  265. *
  266. * @param mixed $offset <p>
  267. * An offset to check for.
  268. * </p>
  269. *
  270. * @return bool true on success or false on failure.
  271. * The return value will be casted to boolean if non-boolean was returned
  272. */
  273. public function offsetExists($offset): bool
  274. {
  275. return $this->has($offset);
  276. }
  277. /**
  278. * (PHP 5 &gt;= 5.0.0)<br/>
  279. * Offset to unset.
  280. *
  281. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  282. *
  283. * @param mixed $offset <p>
  284. * The offset to unset.
  285. * </p>
  286. */
  287. public function offsetUnset($offset): void
  288. {
  289. if ($this->offsetExists($offset)) {
  290. $this->forget($offset);
  291. }
  292. }
  293. /**
  294. * (PHP 5 &gt;= 5.0.0)<br/>
  295. * Offset to retrieve.
  296. *
  297. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  298. *
  299. * @param mixed $offset <p>
  300. * The offset to retrieve.
  301. * </p>
  302. *
  303. * @return mixed Can return all value types
  304. */
  305. #[\ReturnTypeWillChange]
  306. public function offsetGet($offset)
  307. {
  308. return $this->offsetExists($offset) ? $this->get($offset) : null;
  309. }
  310. /**
  311. * (PHP 5 &gt;= 5.0.0)<br/>
  312. * Offset to set.
  313. *
  314. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  315. *
  316. * @param mixed $offset <p>
  317. * The offset to assign the value to.
  318. * </p>
  319. * @param mixed $value <p>
  320. * The value to set.
  321. * </p>
  322. */
  323. #[\ReturnTypeWillChange]
  324. public function offsetSet($offset, $value)
  325. {
  326. $this->set($offset, $value);
  327. }
  328. /**
  329. * (PHP 8.1+) Magic method for serialization.
  330. *
  331. * @return array
  332. */
  333. public function __serialize(): array
  334. {
  335. return $this->items;
  336. }
  337. /**
  338. * (PHP 8.1+) Magic method for unserialization.
  339. *
  340. * @param array $data
  341. */
  342. public function __unserialize(array $data): void
  343. {
  344. $this->items = $data;
  345. }
  346. }