Collection.php 8.6 KB

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