PEAR2_Net_RouterOS  1.0.0b1
A MikroTik RouterOS client
PEAR2/Net/RouterOS/ResponseCollection.php
Go to the documentation of this file.
00001 <?php
00002 
00022 namespace PEAR2\Net\RouterOS;
00023 
00033 class ResponseCollection implements \ArrayAccess, \SeekableIterator, \Countable
00034 {
00035     
00039     protected $responses = array();
00040     
00044     protected $responseTypes = array();
00045     
00049     protected $responseTags = array();
00050     
00056     protected $argumentMap = null;
00057     
00061     protected $position = 0;
00062     
00068     public function __construct(array $responses)
00069     {
00070         $index = 0;
00071         foreach ($responses as $response) {
00072             if ($response instanceof Response) {
00073                 $this->responseTypes[$index] = $response->getType();
00074                 $this->responseTags[$index] = $response->getTag();
00075                 $this->responses[$index++] = $response;
00076             }
00077         }
00078     }
00079     
00085     public function toArray()
00086     {
00087         return $this->responses;
00088     }
00089 
00095     public function count()
00096     {
00097         return count($this->responses);
00098     }
00099 
00107     public function offsetExists($offset)
00108     {
00109         return array_key_exists($offset, $this->responses);
00110     }
00111 
00119     public function offsetGet($offset)
00120     {
00121         return $this->responses[$offset];
00122     }
00123 
00135     public function offsetSet($offset, $value)
00136     {
00137         
00138     }
00139 
00140 
00151     public function offsetUnset($offset)
00152     {
00153         
00154     }
00155 
00162     public function rewind()
00163     {
00164         return $this->seek(0);
00165     }
00166 
00175     public function seek($position)
00176     {
00177         $this->position = $position;
00178         return $this->current();
00179     }
00180 
00187     public function next()
00188     {
00189         ++$this->position;
00190         return $this->current();
00191     }
00192 
00199     public function current()
00200     {
00201         return $this->valid() ? $this->responses[$this->position] : false;
00202     }
00203 
00210     public function key()
00211     {
00212         return $this->valid() ? $this->position : false;
00213     }
00214 
00220     public function valid()
00221     {
00222         return $this->offsetExists($this->position);
00223     }
00224     
00233     public function getArgumentMap()
00234     {
00235         if (null === $this->argumentMap) {
00236             $arguments = array();
00237             foreach ($this->responses as $index => $response) {
00238                 foreach (array_keys($response->getAllArguments()) as $name) {
00239                     if (!isset($arguments[$name])) {
00240                         $arguments[$name] = array();
00241                     }
00242                     $arguments[$name][] = $index;
00243                 }
00244             }
00245             $this->argumentMap = $arguments;
00246         }
00247         return $this->argumentMap;
00248     }
00249     
00259     public function getAllOfType($type)
00260     {
00261         $result = array();
00262         foreach (array_keys($this->responseTypes, $type, true) as $index) {
00263             $result[] = $this->responses[$index];
00264         }
00265         return new ResponseCollection($result);
00266     }
00267     
00276     public function getAllTagged($tag)
00277     {
00278         $result = array();
00279         foreach (array_keys($this->responseTags, $tag, true) as $index) {
00280             $result[] = $this->responses[$index];
00281         }
00282         return new ResponseCollection($result);
00283     }
00284     
00290     public function getLast()
00291     {
00292         return $this->responses[count($this->responses) - 1];
00293     }
00294     
00307     public function __call($method, array $args)
00308     {
00309         return call_user_func_array(
00310             array($this->current(), $method), $args
00311         );
00312     }
00313 
00314 }