morrisonlevi

Untitled

Oct 18th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. class ArrayList implements ArrayAccess, IteratorAggregate, Countable {
  4.    
  5.     private $list = array();
  6.    
  7.     function add($value, $index=null) {
  8.         $this->offsetSet($index, $value);
  9.     }
  10.    
  11.     function addAll(array &$values) {
  12.         foreach($values as &$value) {
  13.             $this->add($value);
  14.         }
  15.     }
  16.    
  17.     function clear() {
  18.         $this->list = array();
  19.     }
  20.    
  21.     function contains($value) {
  22.         return in_array($this->list);
  23.     }
  24.    
  25.     function indexOf($value) {
  26.         foreach($this->list as $index=>$sValue) {
  27.             if($value===$sValue) {
  28.                 return $index;
  29.             }
  30.         }
  31.        
  32.         return -1;
  33.     }
  34.    
  35.     function isEmpty() {
  36.         return $this->count() == 0;
  37.     }
  38.    
  39.     function count() {
  40.         return count($this->list);
  41.     }
  42.    
  43.     function get($index) {
  44.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  45.             throw new InvalidArgumentException(
  46.                 "Index to ".get_class($this)." must be an integer."
  47.             );
  48.         }
  49.        
  50.         $this->offsetGet($index);
  51.        
  52.     }
  53.    
  54.     function remove($index) {
  55.         $this->offsetUnset($index);
  56.     }
  57.    
  58.     function set($index, $value) {
  59.         $this->offsetSet($index, $value);
  60.     }
  61.    
  62.     function size() {
  63.         return $this->count();
  64.     }
  65.    
  66.     function toArray() {
  67.         return $this->list;
  68.     }
  69.  
  70.     function offsetExists($offset) {
  71.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  72.             throw new InvalidArgumentException(
  73.                 "Index to ".get_class($this)." must be an integer."
  74.             );
  75.         }
  76.        
  77.         return array_key_exists($offset, $this->list);
  78.     }
  79.     function offsetGet($offset) {
  80.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  81.             throw new InvalidArgumentException(
  82.                 "Index to ".get_class($this)." must be an integer."
  83.             );
  84.         }
  85.    
  86.         if (offsetExists($offset))
  87.             return $this->list[$offset];
  88.            
  89.         throw new OutOfBoundsException("Index '$offset' does not exist.");
  90.     }
  91.     function offsetSet($offset, $value) {
  92.         if ($offset==null) {
  93.             $this->list[] = $value;
  94.         } else{
  95.             if (!is_numeric($offset) || $offset!=(int)$offset) {
  96.                 throw new InvalidArgumentException(
  97.                     "Index to ".get_class($this)." must be an integer."
  98.                 );
  99.             }
  100.            
  101.             if ($offset<0 || $offset>=$this->size())
  102.                 throw new OutOfBoundsException("Index '$offset' does not exist.");
  103.            
  104.             $this->list[$offset] = $value;
  105.         }
  106.        
  107.     }
  108.     function offsetUnset($offset) {
  109.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  110.             throw new InvalidArgumentException(
  111.                 "Index to ".get_class($this)." must be an integer."
  112.             );
  113.         }
  114.         unset($this->list[$offset]);
  115.         $this->list = array_values($this->list);
  116.     }
  117.     function getIterator() {
  118.         return new ArrayIterator($this->list);
  119.     }
  120.  
  121. }
  122.  
  123. $list = new ArrayList();
  124.  
  125. $list->add(1);
  126.  
  127. try {
  128.     $list[1] = 2;
  129.  
  130.     echo "'\$list[1] = 2' should have thrown an OutOfBoundsError(), did not");
  131.     exit(1);
  132. } catch (OutOfBoundsException $error) {}
  133.  
  134. $list[] = 2;
  135.  
  136. if($list->get(1)!=2)
  137.     echo "'\$list->get(1)' did not return '2', returned '{$list->get(1)}'";
  138.  
  139. if($list->size()!=2)
  140.     echo "'\$list->size()' did not return '2', returned '{$list->size()}'";
  141.  
  142.  
  143. ?>
  144.  
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment