data-tools.php

Generic set of data tools
git clone git://git.finwo.net/lib/data-tools.php
Log | Files | Refs | README

Mappable.php (2067B)


      1 <?php
      2 
      3 namespace Finwo\Datatools;
      4 
      5 use Finwo\PropertyAccessor\PropertyAccessor;
      6 
      7 class Mappable implements \Iterator
      8 {
      9     /**
     10      * @var array
     11      */
     12     protected $indexes = array();
     13 
     14     /**
     15      * @var integer
     16      */
     17     protected $currentIndex = 0;
     18 
     19     /**
     20      * @var PropertyAccessor
     21      */
     22     protected $accessor = null;
     23 
     24     /**
     25      * Mappable constructor.
     26      * @param null $data
     27      */
     28     public function __construct( $data = null )
     29     {
     30         // Build index
     31         $reflect = new \ReflectionClass($this);
     32         $this->indexes = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
     33 
     34         // Map stuff if needed
     35         if ( is_array($data) || is_object($data) ) {
     36             foreach($data as $key => $value) {
     37                 $this->getPropertyAccessor()->set($this, $key, $value);
     38             }
     39         }
     40     }
     41 
     42     /**
     43      * @return PropertyAccessor
     44      */
     45     protected function getPropertyAccessor()
     46     {
     47         if (! $this->accessor instanceof PropertyAccessor ) {
     48             // Run in debug mode, because we might have inaccessible stuff
     49             $this->accessor = new PropertyAccessor(true);
     50         }
     51         return $this->accessor;
     52     }
     53 
     54     /**
     55      * @return array|mixed|null
     56      * @throws \Exception
     57      */
     58     public function current()
     59     {
     60         $name = $this->indexes[$this->currentIndex]->name;
     61         return $this->getPropertyAccessor()->get($this, $name);
     62     }
     63 
     64     /**
     65      * {@inheritdoc}
     66      */
     67     public function next()
     68     {
     69         $this->currentIndex ++;
     70     }
     71 
     72     /**
     73      * @return integer
     74      */
     75     public function key()
     76     {
     77         return $this->currentIndex;
     78     }
     79 
     80     /**
     81      * @return boolean
     82      */
     83     public function valid()
     84     {
     85         return isset($this->indexes[$this->currentIndex]);
     86     }
     87 
     88     /**
     89      * {@inheritdoc}
     90      */
     91     public function rewind()
     92     {
     93         $this->currentIndex = 0;
     94     }
     95 
     96     /**
     97      * {@inheritdoc}
     98      */
     99     public function reverse()
    100     {
    101         $this->indexes = array_reverse($this->indexes);
    102         $this->rewind();
    103     }
    104 }