Advertisement
Guest User

AbstractEntity

a guest
Jul 24th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Application\Entity;
  4.  
  5. abstract class AbstractEntity
  6. {
  7.  
  8.     /**
  9.      * inputFilter
  10.      *
  11.      * @var InputFilter
  12.      * @access protected
  13.      */
  14.     protected $inputFilter;
  15.    
  16.     /**
  17.      * __construct function.
  18.      *
  19.      * @access public
  20.      * @param array $options (default: array())
  21.      * @return void
  22.      */
  23.     public function __construct($options = array())
  24.     {
  25.         $this->setOptions($options);
  26.     }
  27.    
  28.     /** TEMP **/
  29.     /**
  30.      * __call function.
  31.      *
  32.      * @access public
  33.      * @param mixed $method
  34.      * @param mixed $args
  35.      * @return void
  36.      */
  37.     public function __call($method, $args)
  38.     {
  39.         if (strpos($method, 'get') !== false) {
  40.             $property = strtolower(str_replace('get', '', $method));
  41.            
  42.             if (property_exists($this, $property)) {
  43.                 return $this->{$property};
  44.             } else {
  45.                 throw new \Exception('Property not valid.');
  46.             }
  47.         }
  48.     }
  49.    
  50.     /**
  51.      * set function.
  52.      *
  53.      * @access public
  54.      * @param string $key
  55.      * @param mixed $value
  56.      * @return void
  57.      */
  58.     public function set($key, $value)
  59.     {
  60.         if (property_exists($this, $key)) {
  61.             $this->{$key} = $val;
  62.         }
  63.     }
  64.     /** /TEMP **/
  65.        
  66.     /**
  67.      * __get function.
  68.      *
  69.      * @access public
  70.      * @param mixed $propertyName
  71.      * @return void
  72.      */
  73.     public function __get($propertyName)
  74.     {
  75.         $methodName = 'get' . ucfirst($propertyName);
  76.         if (method_exists($this, $methodName)) {
  77.             return $this->$methodName();
  78.         } else {
  79.             throw new \Exception('Attempting to get an invalid property.');
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * __set function.
  85.      *
  86.      * @access public
  87.      * @param string $propertyName
  88.      * @param mixed $propertyValue
  89.      * @return AbstractEntity
  90.      */
  91.     public function __set($propertyName, $propertyValue)
  92.     {
  93.         $methodName = 'set' . ucfirst($propertyName);
  94.         if (method_exists($this, $methodName)) {
  95.             $this->$methodName($propertyValue);
  96.             return $this;
  97.         } else {
  98.             throw new \Exception('Attempting to set an invalid property.');
  99.         }
  100.     }
  101.    
  102.     /**
  103.      * setOptions function.
  104.      *
  105.      * @access public
  106.      * @param array $options (default: array())
  107.      * @return AbstractEntity
  108.      */
  109.     public function setOptions($options = array())
  110.     {
  111.         foreach ($options as $propertyName => $propertyValue) {
  112.             $methodName = 'set' . ucfirst($propertyName);
  113.             if (method_exists($this, $methodName)) {
  114.                 $this->$methodName($propertyValue);
  115.             }
  116.         }
  117.         return $this;
  118.     }
  119.    
  120.     /**
  121.      * getArrayCopy function.
  122.      *
  123.      * @access public
  124.      * @return array
  125.      */
  126.     public function getArrayCopy()
  127.     {
  128.         return get_object_vars($this);
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement