Guest User

Untitled

a guest
Oct 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.26 KB | None | 0 0
  1. <?php
  2. namespace Ext;
  3.  
  4. abstract class Model
  5. {
  6.     private $_isInizialized = false;
  7.     private $_oReflection;
  8.  
  9.     protected function _init()
  10.     {
  11.         if ($this->_isInizialized)
  12.         {
  13.             return;
  14.         }
  15.  
  16.         $sCalledClassName = get_called_class();
  17.  
  18.         $oReflection = new \ReflectionClass($sCalledClassName);
  19.  
  20.         /**
  21.          * hack for auto docrine proxies
  22.          * This is need for next situations:
  23.          * if model extends like
  24.          *     proxy -> entity -> model
  25.          * We will not able to create right Refleaction class
  26.          * becouse get_called_class() will containt
  27.          * first class (from example above - it will be "proxy" class)
  28.          * so it will not b able to access Model properties
  29.          * using reflection.
  30.          *
  31.          * To resolve this problem we get Reflaction of parent class
  32.          * and in loop sort out while name of parent class will
  33.          * be a Etx\Model.
  34.          */        
  35.         while(1)
  36.         {
  37.             $oParentReflectionClass = $oReflection->getparentClass();          
  38.  
  39.             if ($oParentReflectionClass->name == get_class())
  40.             {
  41.                
  42.                 $this->_oReflection = $oReflection;
  43.  
  44.                 break;
  45.             }
  46.             else
  47.             {
  48.                 $oReflection = $oParentReflectionClass;
  49.             }
  50.         }
  51.  
  52.     }
  53.  
  54.     private function _getMethodAction($sMethod)
  55.     {
  56.         $sActionExpression = '/^(get|set|add|)/';
  57.  
  58.         if (preg_match($sActionExpression, $sMethod, $aOutput))
  59.         {
  60.             return $aOutput[1];
  61.         }
  62.  
  63.         return false;
  64.     }
  65.  
  66.     private function _getPeropertyFromMethodName($sMethod)
  67.     {
  68.         $aPropertyParts = preg_split('/(?=[A-Z])/', $sMethod, -1, PREG_SPLIT_NO_EMPTY);
  69.  
  70.         $aPropertyParts = array_map('strtolower', $aPropertyParts);
  71.    
  72.         return implode('_', $aPropertyParts);
  73.     }
  74.  
  75.     private function _getReflactionObject()
  76.     {
  77.         return $this->_oReflection;
  78.     }
  79.  
  80.     public function __call($sMethod, $aArguments)
  81.     {  
  82.         $this->_init();
  83.  
  84.         $mAction = $this->_getMethodAction($sMethod);
  85.  
  86.         if (!$mAction)
  87.         {
  88.             throw new \Exception('No method ' . $sMethod);
  89.         }
  90.  
  91.         $sMethodWithoutAction = str_replace($mAction, '', $sMethod);
  92.  
  93.         $sProperty = $this->_getPeropertyFromMethodName($sMethodWithoutAction);
  94.        
  95.         $sMagicMethod = '_'.$mAction.'Magic';
  96.  
  97.         $oReflection = $this->_getReflactionObject();
  98.  
  99.         $oReflection = $this->_getReflactionObject();
  100.  
  101.         $oProperty = $oReflection->getProperty($sProperty);
  102.         $oProperty->setAccessible(true);
  103.  
  104.         if (!$oProperty)
  105.         {
  106.             throw new \Exception('No Property');
  107.         }
  108.  
  109.         return $this->$sMagicMethod($oProperty, $aArguments);
  110.     }
  111.  
  112.     private function _getMagic($oProperty, $aArguments)
  113.     {  
  114.         $mPropertyValue = $oProperty->getValue($this);
  115.  
  116.         if (is_null($mPropertyValue) && isset($aArguments[0]))
  117.         {
  118.             return $aArguments[0];
  119.         }
  120.  
  121.         return $mPropertyValue;
  122.     }
  123.  
  124.     private function _setMagic($oProperty, $aArguments)
  125.     {  
  126.         if (!isset($aArguments[0]))
  127.         {
  128.             throw new \Exception('No Value for Property');
  129.         }
  130.  
  131.         $oProperty->setValue($this, $aArguments[0]);
  132.  
  133.         return true;
  134.     }
  135.  
  136.     private function _addMagic($oProperty, $aArguments)
  137.     {  
  138.         $aPropertyValue = $this->_getMagic($oProperty, $aArguments);
  139.  
  140.         if (!is_array($aPropertyValue))
  141.         {
  142.             throw new \Exception("Property type is not array");        
  143.         }
  144.  
  145.         if (!isset($aArguments[0]))
  146.         {
  147.             throw new \Exception('No Value for Property');
  148.         }
  149.  
  150.         $aPropertyValue[] = $aArguments[0];
  151.  
  152.         $oProperty->setValue($this, $aPropertyValue);
  153.  
  154.         return true;
  155.     }
  156. }
Add Comment
Please, Sign In to add comment