You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

409 lines
8.2 KiB

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