CList.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * Link : http://www.phpcorner.net
  4. * User : qingbing<780042175@qq.com>
  5. * Date : 2018-12-10
  6. * Version : 1.0
  7. */
  8. namespace Helper;
  9. use Abstracts\Base;
  10. use Traversable;
  11. /**
  12. * \ArrayAccess 将数组提供出来像数组一样使用
  13. * $myList['name'] = $obj; 对应使用 \ArrayAccess.offsetSet($offset, $value)
  14. * $obj = $myList['name']; 对应使用 \ArrayAccess.offsetGet($offset)
  15. * isset($myList['name']); 对应使用 \ArrayAccess.offsetExists($offset)
  16. * unset($myList['name']); 对应使用 \ArrayAccess.offsetUnset($offset)
  17. *
  18. * \Countable 将提供对象的countable功能
  19. * count($myList); 将对应使用 \Countable.count()
  20. *
  21. * \IteratorAggregate 将提供对象支持 foreach 等的迭代操作
  22. * foreach($myList as $key=>$value); 对应使用 \IteratorAggregate.getIterator(),返回必须可迭代
  23. *
  24. * Class CList
  25. * @package Helper
  26. */
  27. class CList extends Base implements \ArrayAccess, \Countable, \IteratorAggregate
  28. {
  29. /* @var array list的对象集合 */
  30. private $_data = [];
  31. /* @var int 列表当前的统计数量 */
  32. private $_count = 0;
  33. /* @var bool list是否只读,默认可读写 */
  34. private $_readOnly = false;
  35. /**
  36. * 构造函数
  37. * @param mixed $data
  38. * @param bool $readOnly
  39. * @throws Exception
  40. */
  41. public function __construct($data = null, $readOnly = false)
  42. {
  43. if (null !== $data) {
  44. $this->copyFrom($data);
  45. }
  46. $this->setReadOnly($readOnly);
  47. }
  48. /**
  49. * 返回 list 是否只读
  50. * @return boolean
  51. */
  52. public function getReadOnly()
  53. {
  54. return $this->_readOnly;
  55. }
  56. /**
  57. * 设置 list 是否只读
  58. * @param boolean $readOnly
  59. */
  60. public function setReadOnly($readOnly)
  61. {
  62. $this->_readOnly = $readOnly;
  63. }
  64. /**
  65. * 从可迭代数据中复制成 list
  66. * @param mixed $data
  67. * @throws Exception
  68. */
  69. public function copyFrom($data)
  70. {
  71. if (is_array($data) || ($data instanceof \Traversable)) {
  72. if ($this->_count > 0) {
  73. $this->clear();
  74. }
  75. if ($data instanceof CList) {
  76. $data = $data->_data;
  77. }
  78. foreach ($data as $item) {
  79. $this->push($item);
  80. }
  81. } elseif ($data !== null) {
  82. throw new Exception('实例化列表参数必须是数组或可遍历的对象', 100100501);
  83. }
  84. }
  85. /**
  86. * list 合并
  87. * @param mixed $data
  88. * @throws Exception
  89. */
  90. public function mergeWith($data)
  91. {
  92. if (is_array($data) || ($data instanceof \Traversable)) {
  93. if ($data instanceof CList) {
  94. $data = $data->_data;
  95. }
  96. foreach ($data as $item) {
  97. $this->push($item);
  98. }
  99. } elseif ($data !== null) {
  100. throw new Exception('列表合并参数必须是数组或可遍历的对象', 100100502);
  101. }
  102. }
  103. /**
  104. * 获取 list 的长度
  105. * @return int
  106. */
  107. public function getCount()
  108. {
  109. return $this->count();
  110. }
  111. /**
  112. * 返回元素在 list 中的索引
  113. * @param mixed $item
  114. * @return int|mixed 未找到返回-1
  115. */
  116. protected function indexOf($item)
  117. {
  118. if (false !== ($index = array_search($item, $this->_data, true)))
  119. return $index;
  120. else
  121. return -1;
  122. }
  123. /**
  124. * 在指定位置插入一个元素
  125. * @param int $index
  126. * @param mixed $item
  127. * @throws Exception
  128. */
  129. protected function insertAt($index, $item)
  130. {
  131. if ($this->getReadOnly()) {
  132. throw new Exception('只读list,不允许执行插入操作', 100100503);
  133. }
  134. if ($this->_count === $index) {
  135. $this->_data[$this->_count++] = $item;
  136. } elseif ($index >= 0 && $index < $this->_count) {
  137. array_splice($this->_data, $index, 0, [$item]);
  138. $this->_count++;
  139. } else {
  140. throw new Exception(str_cover('List索引"{index}"已超出范围', [
  141. '{index}' => $index,
  142. ]), 100100504);
  143. }
  144. }
  145. /**
  146. * 删除一个 list 元素,并返回这个元素
  147. * @param int $index
  148. * @return mixed
  149. * @throws Exception
  150. */
  151. protected function removeAt($index)
  152. {
  153. if ($this->getReadOnly()) {
  154. throw new Exception('只读list,不允许执行移除操作', 100100505);
  155. }
  156. if ($index >= 0 && $index < $this->_count) {
  157. $this->_count--;
  158. if ($index === $this->_count) {
  159. return array_pop($this->_data);
  160. } else {
  161. $item = $this->_data[$index];
  162. array_splice($this->_data, $index, 1);
  163. return $item;
  164. }
  165. } else {
  166. throw new Exception(str_cover('List索引"{index}"已超出范围', [
  167. '{index}' => $index,
  168. ]), 100100506);
  169. }
  170. }
  171. /**
  172. * 获取 list 中指定索引的元素
  173. * @param int $index
  174. * @return mixed
  175. * @throws Exception
  176. */
  177. protected function itemAt($index)
  178. {
  179. if (isset($this->_data[$index])) {
  180. return $this->_data[$index];
  181. } elseif ($index >= 0 && $index < $this->_count) {
  182. return $this->_data[$index];
  183. } else {
  184. throw new Exception(str_cover('List索引"{index}"已超出范围', [
  185. '{index}' => $index,
  186. ]), 100100507);
  187. }
  188. }
  189. /**
  190. * 返回是否包含元素
  191. * @param mixed $item
  192. * @return boolean
  193. */
  194. public function contains($item)
  195. {
  196. return $this->indexOf($item) >= 0;
  197. }
  198. /**
  199. * 添加一个元素,返回添加的元素索引
  200. * @param mixed $item
  201. * @return int
  202. * @throws Exception
  203. */
  204. public function push($item)
  205. {
  206. $this->insertAt($this->_count, $item);
  207. return $this->_count - 1;
  208. }
  209. /**
  210. * 移除最后的元素,并返回
  211. * @return mixed
  212. * @throws Exception
  213. */
  214. public function pop()
  215. {
  216. return $this->removeAt($this->_count - 1);
  217. }
  218. /**
  219. * 向列表头添加一个元素,成功返回添加的元素索引
  220. * @param mixed $item
  221. * @return int
  222. * @throws Exception
  223. */
  224. public function unshift($item)
  225. {
  226. $this->insertAt(0, $item);
  227. return 0;
  228. }
  229. /**
  230. * 移除第一个元素,并返回
  231. * @return mixed
  232. * @throws Exception
  233. */
  234. public function shift()
  235. {
  236. return $this->removeAt(0);
  237. }
  238. /**
  239. * 删除一个元素,返回元素在 list 中的索引,未找到返回 false
  240. * @param mixed $item
  241. * @return bool|int|mixed
  242. * @throws Exception
  243. */
  244. public function remove($item)
  245. {
  246. if (($index = $this->indexOf($item)) >= 0) {
  247. $this->removeAt($index);
  248. return $index;
  249. } else
  250. return false;
  251. }
  252. /**
  253. * 清除 list 的所有元素
  254. * @throws Exception
  255. */
  256. public function clear()
  257. {
  258. for ($i = $this->_count - 1; $i >= 0; $i--) {
  259. $this->removeAt($i);
  260. }
  261. }
  262. /**
  263. * Whether a offset exists
  264. * @link http://php.net/manual/en/arrayaccess.offsetexists.php
  265. * @param mixed $offset <p>
  266. * An offset to check for.
  267. * </p>
  268. * @return boolean true on success or false on failure.
  269. * </p>
  270. * <p>
  271. * The return value will be casted to boolean if non-boolean was returned.
  272. * @since 5.0.0
  273. */
  274. public function offsetExists($offset)
  275. {
  276. return ($offset >= 0 && $offset < $this->_count);
  277. }
  278. /**
  279. * Offset to retrieve
  280. * @link http://php.net/manual/en/arrayaccess.offsetget.php
  281. * @param mixed $offset <p>
  282. * The offset to retrieve.
  283. * </p>
  284. * @return mixed Can return all value types.
  285. * @since 5.0.0
  286. * @throws Exception
  287. */
  288. public function offsetGet($offset)
  289. {
  290. return $this->itemAt($offset);
  291. }
  292. /**
  293. * Offset to set
  294. * @link http://php.net/manual/en/arrayaccess.offsetset.php
  295. * @param mixed $offset <p>
  296. * The offset to assign the value to.
  297. * </p>
  298. * @param mixed $value <p>
  299. * The value to set.
  300. * </p>
  301. * @return void
  302. * @since 5.0.0
  303. * @throws Exception
  304. */
  305. public function offsetSet($offset, $value)
  306. {
  307. if (null === $offset || $offset === $this->_count) {
  308. $this->insertAt($this->_count, $value);
  309. } else {
  310. $this->removeAt($offset);
  311. $this->insertAt($offset, $value);
  312. }
  313. }
  314. /**
  315. * Offset to unset
  316. * @link http://php.net/manual/en/arrayaccess.offsetunset.php
  317. * @param mixed $offset <p>
  318. * The offset to unset.
  319. * </p>
  320. * @return void
  321. * @since 5.0.0
  322. * @throws Exception
  323. */
  324. public function offsetUnset($offset)
  325. {
  326. $this->removeAt($offset);
  327. }
  328. /**
  329. * Count elements of an object
  330. * @link http://php.net/manual/en/countable.count.php
  331. * @return int The custom count as an integer.
  332. * </p>
  333. * <p>
  334. * The return value is cast to an integer.
  335. * @since 5.1.0
  336. */
  337. public function count()
  338. {
  339. return $this->_count;
  340. }
  341. /**
  342. * Retrieve an external iterator
  343. * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
  344. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  345. * <b>Traversable</b>
  346. * @since 5.0.0
  347. */
  348. public function getIterator()
  349. {
  350. return new CListIterator($this->_data);
  351. }
  352. }