Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Ext;
- abstract class Model
- {
- private $_isInizialized = false;
- private $_oReflection;
- protected function _init()
- {
- if ($this->_isInizialized)
- {
- return;
- }
- $sCalledClassName = get_called_class();
- $oReflection = new \ReflectionClass($sCalledClassName);
- /**
- * hack for auto docrine proxies
- * This is need for next situations:
- * if model extends like
- * proxy -> entity -> model
- * We will not able to create right Refleaction class
- * becouse get_called_class() will containt
- * first class (from example above - it will be "proxy" class)
- * so it will not b able to access Model properties
- * using reflection.
- *
- * To resolve this problem we get Reflaction of parent class
- * and in loop sort out while name of parent class will
- * be a Etx\Model.
- */
- while(1)
- {
- $oParentReflectionClass = $oReflection->getparentClass();
- if ($oParentReflectionClass->name == get_class())
- {
- $this->_oReflection = $oReflection;
- break;
- }
- else
- {
- $oReflection = $oParentReflectionClass;
- }
- }
- }
- private function _getMethodAction($sMethod)
- {
- $sActionExpression = '/^(get|set|add|)/';
- if (preg_match($sActionExpression, $sMethod, $aOutput))
- {
- return $aOutput[1];
- }
- return false;
- }
- private function _getPeropertyFromMethodName($sMethod)
- {
- $aPropertyParts = preg_split('/(?=[A-Z])/', $sMethod, -1, PREG_SPLIT_NO_EMPTY);
- $aPropertyParts = array_map('strtolower', $aPropertyParts);
- return implode('_', $aPropertyParts);
- }
- private function _getReflactionObject()
- {
- return $this->_oReflection;
- }
- public function __call($sMethod, $aArguments)
- {
- $this->_init();
- $mAction = $this->_getMethodAction($sMethod);
- if (!$mAction)
- {
- throw new \Exception('No method ' . $sMethod);
- }
- $sMethodWithoutAction = str_replace($mAction, '', $sMethod);
- $sProperty = $this->_getPeropertyFromMethodName($sMethodWithoutAction);
- $sMagicMethod = '_'.$mAction.'Magic';
- $oReflection = $this->_getReflactionObject();
- $oReflection = $this->_getReflactionObject();
- $oProperty = $oReflection->getProperty($sProperty);
- $oProperty->setAccessible(true);
- if (!$oProperty)
- {
- throw new \Exception('No Property');
- }
- return $this->$sMagicMethod($oProperty, $aArguments);
- }
- private function _getMagic($oProperty, $aArguments)
- {
- $mPropertyValue = $oProperty->getValue($this);
- if (is_null($mPropertyValue) && isset($aArguments[0]))
- {
- return $aArguments[0];
- }
- return $mPropertyValue;
- }
- private function _setMagic($oProperty, $aArguments)
- {
- if (!isset($aArguments[0]))
- {
- throw new \Exception('No Value for Property');
- }
- $oProperty->setValue($this, $aArguments[0]);
- return true;
- }
- private function _addMagic($oProperty, $aArguments)
- {
- $aPropertyValue = $this->_getMagic($oProperty, $aArguments);
- if (!is_array($aPropertyValue))
- {
- throw new \Exception("Property type is not array");
- }
- if (!isset($aArguments[0]))
- {
- throw new \Exception('No Value for Property');
- }
- $aPropertyValue[] = $aArguments[0];
- $oProperty->setValue($this, $aPropertyValue);
- return true;
- }
- }
Add Comment
Please, Sign In to add comment