123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace Org\Util;
- class ArrayList implements \IteratorAggregate {
-
- protected $_elements = array();
-
- public function __construct($elements = array()) {
- if (!empty($elements)) {
- $this->_elements = $elements;
- }
- }
-
- public function getIterator() {
- return new ArrayObject($this->_elements);
- }
-
- public function add($element) {
- return (array_push($this->_elements, $element)) ? true : false;
- }
-
- public function unshift($element) {
- return (array_unshift($this->_elements,$element))?true : false;
- }
-
- public function pop() {
- return array_pop($this->_elements);
- }
-
- public function addAll($list) {
- $before = $this->size();
- foreach( $list as $element) {
- $this->add($element);
- }
- $after = $this->size();
- return ($before < $after);
- }
-
- public function clear() {
- $this->_elements = array();
- }
-
- public function contains($element) {
- return (array_search($element, $this->_elements) !== false );
- }
-
- public function get($index) {
- return $this->_elements[$index];
- }
-
- public function indexOf($element) {
- return array_search($element, $this->_elements);
- }
-
- public function isEmpty() {
- return empty($this->_elements);
- }
-
- public function lastIndexOf($element) {
- for ($i = (count($this->_elements) - 1); $i > 0; $i--) {
- if ($element == $this->get($i)) { return $i; }
- }
- }
- public function toJson() {
- return json_encode($this->_elements);
- }
-
- public function remove($index) {
- $element = $this->get($index);
- if (!is_null($element)) { array_splice($this->_elements, $index, 1); }
- return $element;
- }
-
- public function removeRange($offset , $length) {
- array_splice($this->_elements, $offset , $length);
- }
-
- public function unique() {
- $this->_elements = array_unique($this->_elements);
- }
-
- public function range($offset,$length=null) {
- return array_slice($this->_elements,$offset,$length);
- }
-
- public function set($index, $element) {
- $previous = $this->get($index);
- $this->_elements[$index] = $element;
- return $previous;
- }
-
- public function size() {
- return count($this->_elements);
- }
-
- public function toArray() {
- return $this->_elements;
- }
-
- public function ksort() {
- ksort($this->_elements);
- }
-
- public function asort() {
- asort($this->_elements);
- }
-
- public function rsort() {
- rsort($this->_elements);
- }
-
- public function natsort() {
- natsort($this->_elements);
- }
- }
|