vendor\project-biz\portal-bundle\src\View\TableView\Accessor\KeyBasedAccessor.php line 60

Open in your IDE?
  1. <?php
  2. namespace ProjectBiz\PortalBundle\View\TableView\Accessor;
  3. use ProjectBiz\PortalBundle\View\TableView\HasKeys;
  4. use ProjectBiz\PortalBundle\View\TableView\HasRawData;
  5. class KeyBasedAccessor implements \Iterator\ArrayAccessHasKeysHasRawData
  6. {
  7.     protected $data;
  8.     private $keys;
  9.     private $currentIndex;
  10.     public function __construct(
  11.         $data null
  12.     ) {
  13.         $this->data $data;
  14.         $this->currentIndex = -1;
  15.     }
  16.     /*
  17.      * Iterator
  18.      */
  19.     public function current()
  20.     {
  21.         return $this[$this->getKeys()[$this->currentIndex]];
  22.     }
  23.     public function next()
  24.     {
  25.         ++$this->currentIndex;
  26.     }
  27.     public function key()
  28.     {
  29.         return $this->getKeys()[$this->currentIndex];
  30.     }
  31.     public function valid()
  32.     {
  33.         return ($this->currentIndex >=0) && ($this->currentIndex count($this->getKeys()));
  34.     }
  35.     public function rewind()
  36.     {
  37.         $this->currentIndex 0;
  38.     }
  39.     /*
  40.      * ArrayAccess
  41.      */
  42.     public function offsetExists($offset)
  43.     {
  44.         return array_key_exists($offset$this->getKeys());
  45.     }
  46.     public function offsetGet($offset)
  47.     {
  48.         return $this->data[$offset];
  49.     }
  50.     public function offsetSet($offset$value)
  51.     {
  52.         throw new \Exception('Unable to set value in read-only array');
  53.     }
  54.     public function offsetUnset($offset)
  55.     {
  56.         throw new \Exception('Unable to unset value in read-only array');
  57.     }
  58.     /*
  59.      * HasKeys
  60.      */
  61.     public function getKeys() {
  62.         if (!$this->keys) {
  63.             $this->keys $this->buildKeys();
  64.         }
  65.         return $this->keys;
  66.     }
  67.     /*
  68.      * helpers
  69.      */
  70.     protected function buildKeys() {
  71.         $keys = [];
  72.         if (is_array($this->data)) {
  73.             $keys array_keys($this->data);
  74.         }
  75.         else if($this->data instanceof HasKeys) {
  76.             $keys $this->data->getKeys();
  77.         }
  78.         else if($this->data instanceof \Iterator) {
  79.             $this->data->rewind();
  80.             while ($this->data->valid()) {
  81.                 $keys[] = $this->data->key();
  82.             }
  83.         }
  84.         else {
  85.             foreach ($this->data as $key => $_){
  86.                 $keys[] = $key;
  87.             }
  88.         }
  89.         return $keys;
  90.     }
  91.     function getRawData()
  92.     {
  93.         if ($this->data instanceof HasRawData) {
  94.             return $this->data->getRawData();
  95.         }
  96.         return $this->data;
  97.     }
  98. }