Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Most basic model from the entire system.
- * Has some methods useful to all Model based classes, specially Value Objects.
- *
- * @package eseu
- * @author Augusto Pascutti
- */
- abstract class Model_App
- {
- /**
- * Use as argument to return method result as object
- */
- const RETURN_OBJECT = 'object';
- /**
- * Use as argument to return method result as string
- */
- const RETURN_STRING = 'string';
- /**
- * Primery keys from the database that are present in the model
- *
- * @var array
- */
- private $_keys = array('id');
- /**
- * Define the primary keys in the database that are present as attributes
- * in the Model class.
- * This function does not append keys if there is any existing one,
- * it overwrites the keys with the arguments given to this method.
- * Usage: $this->setKeys('id','userId',...)
- *
- * @param string|array $arg1 Name of attributes that are keys
- * @param string|array[optional] $arg2
- */
- protected function _setKeys()
- {
- $args = func_get_args();
- $this->_keys = array();
- foreach ($args as $argument) {
- if ( is_array($argument) ) { $this->_setKeys($argument); }
- $this->_keys[] = $argument;
- }
- }
- /**
- * Return the keys from this model
- *
- * @see Model_App::setKeys()
- * @throws InvalidArgumentException
- * @return array
- */
- protected function _getKeys()
- {
- if ( empty($this->_keys) || count($this->_keys) <= 0 ) {
- throw new InvalidArgumentException(__('Keys are not set into the current model!'));
- }
- return $this->_keys;
- }
- /**
- * Fake constructor to be used in the model classes
- */
- protected function _init()
- {
- // ...
- }
- /**
- * Class contructor.
- * If an array is given as the first parameter, it executes
- * $this->populate() using the given array.
- * If you need a contructor in the Model class, use the init()
- * method.
- *
- * @magic
- * @see Model_App::populate
- * @param array $array
- */
- final public function __construct($array = null)
- {
- if ( ! is_null($array) && is_array($array) ) {
- $this->populate($array);
- }
- $this->_init();
- }
- /**
- * Defines the attribute (if it exists) using the correct
- * setter for it.
- *
- * @magic
- * @see Model_App::getMethodName()
- * @param string $attr
- * @param mixed $val
- * @return void
- */
- public function __set($attr, $val)
- {
- $method = $this->getMethodName($attr,'set');
- if ( is_null($method) ) {
- return;
- }
- $this->$method($val);
- }
- /**
- * Returns the given attribute value using its correct method.
- *
- * @magic
- * @see Model_App::getMethodName()
- * @param string $attr
- * @return mixed
- */
- public function __get($attr)
- {
- $method = $this->getMethodName($attr,'get');
- return ( is_null($method) ) ? null : $this->$method();
- }
- /**
- * Return the model name without any prefix.
- * Ex: Model_Country retorn Country
- * Model_DbTable_Country return Country
- *
- * @return string
- */
- public function getModelName()
- {
- $fullname = get_class($this);
- $aPieces = explode('_',$fullname);
- $lastname = array_pop($aPieces);
- return $lastname;
- }
- /**
- * Returns the method name that exists for the given attribute
- * in the current object.
- * If NULL is return than there is no method for the attribute given.
- *
- * @param string $attribute
- * @param 'set'|'get'[optional] $prefix (Default: 'set')
- * @return string|null
- */
- public function getMethodName($attribute, $prefix = 'set')
- {
- $model_name = $this->getModelName();
- $method = $prefix.str_replace(strtolower($model_name),'',strtolower($attribute));
- if ( method_exists($this,$method) ) {
- return $method;
- } else if ( ($method = $prefix.$attribute) && method_exists($this,$method) ) {
- return $method;
- }
- return null;
- }
- /**
- * Populates the class atributes using the right setter methods.
- * Useful when you have information from the database and want to
- * transfer to an Value Object.
- *
- * @see Model_App::getMethodName()
- * @param array $a Array passed as an argument array('id'=>'1', 'name'=>'Pascutti', 'login'=>'pascutti')
- */
- public function populate(array $a)
- {
- foreach ($a as $attr=>$value) {
- $method = $this->getMethodName($attr,'set');
- if ( is_null($method) ) { continue; }
- $this->$method($value);
- }
- }
- /**
- * Returns the DAO object for the current model.
- *
- * @throws LogicException
- * @param string[optional] |$returnObject (Default: self::RETURN_OBJECT)
- * @return Model_DbTable_AppAbstract|string
- */
- public function getDaoClass($returnObject = self::RETURN_OBJECT)
- {
- static $instance;
- if ( ! $instance instanceof Model_DbTable_AppAbstract ) {
- $daoName = 'Model_DbTable_'.$this->getModelName();
- if ( ! class_exists($daoName,true) ) {
- $msg = __('DAO Class not found: %s');
- $msg = sprintf($msg,$daoName);
- throw new LogicException(__($msg));
- }
- $instance = new $daoName();
- }
- return ($returnObject == self::RETURN_OBJECT) ? $instance : get_class($instance) ;
- }
- /**
- * Retrieves information from the database to the current Model.
- * It finds the data based on the primary keys of the Model.
- *
- * @return Model_App
- */
- public function getInfo($recursive = false) {
- $aKeys = $this->_getKeys();
- $aKeys = array_flip($aKeys);
- foreach ($aKeys as $key=>$void) {
- $method = $this->getMethodName($key,'get');
- $value = $this->$method();
- if ( empty($value) ) { return $this; }
- $aKeys[$key] = $value;
- }
- $method = 'get'.$this->getModelName();
- $rMethod = new ReflectionMethod($this->getDaoClass(self::RETURN_STRING), $method);
- $data = $rMethod->invokeArgs($this->getDaoClass(), $aKeys);
- $this->populate($data);
- if ( $recursive ) {
- $rClass = new ReflectionClass('Model_'.$this->getModelName());
- foreach ( $rClass->getProperties() as $property ) {
- $method = $this->getMethodName($property->getName(), 'get');
- if ( is_null($method) ) { continue; }
- $result = $this->$method();
- if ( $result instanceof Model_App ) {
- $this->$method()->getInfo(true);
- }
- }
- }
- return $this;
- }
- }
Add Comment
Please, Sign In to add comment