Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. namespace MarcoPivetta;
  2. /**
  3.  * Makes elements accessible as an array
  4.  * Proxies all calls to an array offset to getter/setter methods of the class
  5.  * Inflater getter/setter has to be created
  6.  *
  7.  * @author Ocramius
  8.  */
  9. class Entity implements \ArrayAccess {
  10.  
  11.     /**
  12.      * Empty default constructor to allow parent constructor invocation
  13.      */
  14.     public function __construct(){
  15.  
  16.     }
  17.  
  18.     /**
  19.      * ArrayAccess implementation
  20.      */
  21.     public function offsetExists($offset) {
  22.         return $this->__isset($offset);
  23.     }
  24.  
  25.     /**
  26.      * ArrayAccess implementation
  27.      */
  28.     public function offsetGet($offset) {
  29.         return $this->__get($offset);
  30.     }
  31.  
  32.     /**
  33.      * ArrayAccess implementation
  34.      */
  35.     public function offsetSet($offset, $value) {
  36.         return $this->__set($offset,$value);
  37.     }
  38.  
  39.     /**
  40.      * ArrayAccess implementation
  41.      */
  42.     public function offsetUnset($offset) {
  43.         return $this->__unset($offset);
  44.     }
  45.  
  46.     /**
  47.      * proxy to setter of $name
  48.      */
  49.     public function  __set($name, $value) {
  50.         return $this->{'set'.ucfirst($name)}($value);
  51.     }
  52.  
  53.     /**
  54.      * proxy to getter of $name
  55.      */
  56.     public function  __get($name) {
  57.         return $this->{'get'.ucfirst($name)}();
  58.     }
  59.  
  60.     /**
  61.      * proxy to issetter of $name
  62.      */
  63.     public function  __isset($name) {
  64.         return $this->{'get'.ucfirst($name)}() !== null;
  65.     }
  66.  
  67.     /**
  68.      * proxy to unsetter of $name
  69.      */
  70.     public function  __unset($name) {
  71.         return $this->__set($name,null);
  72.     }
  73.  
  74.     /**
  75.      * Allows usage of set$Parameter when the method is not defined though inflection
  76.      */
  77.     public function  __call($name, $arguments) {
  78.         if(strpos($name,'set')===0){
  79.             return $this->{lcfirst(substr($name,3))}=$arguments[0];
  80.         }elseif(strpos($name,'get')===0){
  81.             return $this->{lcfirst(substr($name,3))};
  82.         }
  83.         //ADD METHODS TO PUSH/POP/ADD/REMOVE ON ARRAY ELEMENTS
  84.         throw new \MarcoPivetta\Exception('Undefined method \'' . $name . '\' called on ' . get_class($this), 6000);
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement