Guest User

Untitled

a guest
Jul 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. <?php
  2. /**
  3. * Most basic model from the entire system.
  4. * Has some methods useful to all Model based classes, specially Value Objects.
  5. *
  6. * @package eseu
  7. * @author Augusto Pascutti
  8. */
  9. abstract class Model_App
  10. {
  11. /**
  12. * Use as argument to return method result as object
  13. */
  14. const RETURN_OBJECT = 'object';
  15. /**
  16. * Use as argument to return method result as string
  17. */
  18. const RETURN_STRING = 'string';
  19.  
  20. /**
  21. * Primery keys from the database that are present in the model
  22. *
  23. * @var array
  24. */
  25. private $_keys = array('id');
  26.  
  27. /**
  28. * Define the primary keys in the database that are present as attributes
  29. * in the Model class.
  30. * This function does not append keys if there is any existing one,
  31. * it overwrites the keys with the arguments given to this method.
  32. * Usage: $this->setKeys('id','userId',...)
  33. *
  34. * @param string|array $arg1 Name of attributes that are keys
  35. * @param string|array[optional] $arg2
  36. */
  37. protected function _setKeys()
  38. {
  39. $args = func_get_args();
  40. $this->_keys = array();
  41. foreach ($args as $argument) {
  42. if ( is_array($argument) ) { $this->_setKeys($argument); }
  43. $this->_keys[] = $argument;
  44. }
  45. }
  46.  
  47. /**
  48. * Return the keys from this model
  49. *
  50. * @see Model_App::setKeys()
  51. * @throws InvalidArgumentException
  52. * @return array
  53. */
  54. protected function _getKeys()
  55. {
  56. if ( empty($this->_keys) || count($this->_keys) <= 0 ) {
  57. throw new InvalidArgumentException(__('Keys are not set into the current model!'));
  58. }
  59. return $this->_keys;
  60. }
  61.  
  62. /**
  63. * Fake constructor to be used in the model classes
  64. */
  65. protected function _init()
  66. {
  67. // ...
  68. }
  69.  
  70. /**
  71. * Class contructor.
  72. * If an array is given as the first parameter, it executes
  73. * $this->populate() using the given array.
  74. * If you need a contructor in the Model class, use the init()
  75. * method.
  76. *
  77. * @magic
  78. * @see Model_App::populate
  79. * @param array $array
  80. */
  81. final public function __construct($array = null)
  82. {
  83. if ( ! is_null($array) && is_array($array) ) {
  84. $this->populate($array);
  85. }
  86. $this->_init();
  87. }
  88.  
  89. /**
  90. * Defines the attribute (if it exists) using the correct
  91. * setter for it.
  92. *
  93. * @magic
  94. * @see Model_App::getMethodName()
  95. * @param string $attr
  96. * @param mixed $val
  97. * @return void
  98. */
  99. public function __set($attr, $val)
  100. {
  101. $method = $this->getMethodName($attr,'set');
  102. if ( is_null($method) ) {
  103. return;
  104. }
  105. $this->$method($val);
  106. }
  107.  
  108. /**
  109. * Returns the given attribute value using its correct method.
  110. *
  111. * @magic
  112. * @see Model_App::getMethodName()
  113. * @param string $attr
  114. * @return mixed
  115. */
  116. public function __get($attr)
  117. {
  118. $method = $this->getMethodName($attr,'get');
  119. return ( is_null($method) ) ? null : $this->$method();
  120. }
  121.  
  122. /**
  123. * Return the model name without any prefix.
  124. * Ex: Model_Country retorn Country
  125. * Model_DbTable_Country return Country
  126. *
  127. * @return string
  128. */
  129. public function getModelName()
  130. {
  131. $fullname = get_class($this);
  132. $aPieces = explode('_',$fullname);
  133. $lastname = array_pop($aPieces);
  134. return $lastname;
  135. }
  136.  
  137. /**
  138. * Returns the method name that exists for the given attribute
  139. * in the current object.
  140. * If NULL is return than there is no method for the attribute given.
  141. *
  142. * @param string $attribute
  143. * @param 'set'|'get'[optional] $prefix (Default: 'set')
  144. * @return string|null
  145. */
  146. public function getMethodName($attribute, $prefix = 'set')
  147. {
  148. $model_name = $this->getModelName();
  149. $method = $prefix.str_replace(strtolower($model_name),'',strtolower($attribute));
  150. if ( method_exists($this,$method) ) {
  151. return $method;
  152. } else if ( ($method = $prefix.$attribute) && method_exists($this,$method) ) {
  153. return $method;
  154. }
  155. return null;
  156. }
  157.  
  158. /**
  159. * Populates the class atributes using the right setter methods.
  160. * Useful when you have information from the database and want to
  161. * transfer to an Value Object.
  162. *
  163. * @see Model_App::getMethodName()
  164. * @param array $a Array passed as an argument array('id'=>'1', 'name'=>'Pascutti', 'login'=>'pascutti')
  165. */
  166. public function populate(array $a)
  167. {
  168. foreach ($a as $attr=>$value) {
  169. $method = $this->getMethodName($attr,'set');
  170. if ( is_null($method) ) { continue; }
  171. $this->$method($value);
  172. }
  173. }
  174.  
  175. /**
  176. * Returns the DAO object for the current model.
  177. *
  178. * @throws LogicException
  179. * @param string[optional] |$returnObject (Default: self::RETURN_OBJECT)
  180. * @return Model_DbTable_AppAbstract|string
  181. */
  182. public function getDaoClass($returnObject = self::RETURN_OBJECT)
  183. {
  184. static $instance;
  185. if ( ! $instance instanceof Model_DbTable_AppAbstract ) {
  186. $daoName = 'Model_DbTable_'.$this->getModelName();
  187. if ( ! class_exists($daoName,true) ) {
  188. $msg = __('DAO Class not found: %s');
  189. $msg = sprintf($msg,$daoName);
  190. throw new LogicException(__($msg));
  191. }
  192. $instance = new $daoName();
  193. }
  194.  
  195. return ($returnObject == self::RETURN_OBJECT) ? $instance : get_class($instance) ;
  196. }
  197.  
  198. /**
  199. * Retrieves information from the database to the current Model.
  200. * It finds the data based on the primary keys of the Model.
  201. *
  202. * @return Model_App
  203. */
  204. public function getInfo($recursive = false) {
  205. $aKeys = $this->_getKeys();
  206. $aKeys = array_flip($aKeys);
  207. foreach ($aKeys as $key=>$void) {
  208. $method = $this->getMethodName($key,'get');
  209. $value = $this->$method();
  210. if ( empty($value) ) { return $this; }
  211. $aKeys[$key] = $value;
  212. }
  213. $method = 'get'.$this->getModelName();
  214. $rMethod = new ReflectionMethod($this->getDaoClass(self::RETURN_STRING), $method);
  215. $data = $rMethod->invokeArgs($this->getDaoClass(), $aKeys);
  216. $this->populate($data);
  217.  
  218. if ( $recursive ) {
  219. $rClass = new ReflectionClass('Model_'.$this->getModelName());
  220. foreach ( $rClass->getProperties() as $property ) {
  221. $method = $this->getMethodName($property->getName(), 'get');
  222. if ( is_null($method) ) { continue; }
  223. $result = $this->$method();
  224. if ( $result instanceof Model_App ) {
  225. $this->$method()->getInfo(true);
  226. }
  227. }
  228. }
  229. return $this;
  230. }
  231. }
Add Comment
Please, Sign In to add comment