PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Business/ModelBusiness.php

https://gitlab.com/phalcony/phalcony-core
PHP | 266 lines | 195 code | 16 blank | 55 comment | 2 complexity | c920506bed6210fb887b43441d2c39c0 MD5 | raw file
  1. <?php
  2. namespace Phalcony\Core\Business;
  3. use Phalcony\Core\Business;
  4. use Phalcony\Core\Business\Validation\Exception\ModelException;
  5. use Phalcony\Core\Model;
  6. use Phalcony\Core\Repository;
  7. use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
  8. class ModelBusiness extends Business
  9. {
  10. /**
  11. * Nome da classe da Repository desta Service
  12. * @var string
  13. */
  14. protected $repositoryClass = null;
  15. /**
  16. * Instância da Repository que está sendo manipulada pela Business
  17. * @var \Model\Core\Repository
  18. */
  19. protected $repository = null;
  20. /**
  21. * Nome da classe da Model desta Service
  22. * @var string
  23. */
  24. protected $modelClass = null;
  25. /**
  26. * Instância da Model que está sendo manipulada pela Business
  27. * @var \Phalcony\Core\Model
  28. */
  29. protected $model = null;
  30. /**
  31. * Service de Business de Validação dos dados do Modelo
  32. *
  33. * @var null
  34. */
  35. protected $modelValidatorClass = null;
  36. /**
  37. * Service de Business de verificação de regras de negócios
  38. *
  39. * @var null
  40. */
  41. protected $ruleValidatorClass = null;
  42. /**
  43. * Define o nome da classe da Repository
  44. *
  45. * @param [type] $repositoryClass [description]
  46. */
  47. public function setRepositoryClass($repositoryClass)
  48. {
  49. $this->repositoryClass = $repositoryClass;
  50. }
  51. /**
  52. * Define o nome da classe da Model
  53. *
  54. * @param [type] $modelClass [description]
  55. */
  56. public function setModelClass($modelClass)
  57. {
  58. $this->modelClass = $modelClass;
  59. }
  60. /**
  61. * Retorna ou cria automaticamente com base no nome da service, o nome da entidade raiz
  62. *
  63. * @return string
  64. */
  65. public function getRelatedClass($attr, $namespace, $sufix = '')
  66. {
  67. if (is_null($this->$attr)) {
  68. $arr = explode('\\', get_class($this));
  69. $arr[count($arr) - 2] = $namespace;
  70. $arr[count($arr) - 1] = str_replace(
  71. array('Service', 'Business'),
  72. $sufix,
  73. array_pop($arr)
  74. );
  75. $this->$attr = implode('\\', $arr);
  76. }
  77. return $this->$attr;
  78. }
  79. /**
  80. * Retorna ou cria automaticamente com base no nome da service, o nome da entidade raiz
  81. *
  82. * @return string
  83. */
  84. public function getModelValidator()
  85. {
  86. $this->getRelatedClass('modelValidatorClass', 'Business\\Validation', 'ModelValidator');
  87. return class_exists($this->modelValidatorClass) ? new $this->modelValidatorClass : null;
  88. }
  89. /**
  90. * Retorna ou cria automaticamente com base no nome da service, o nome da entidade raiz
  91. *
  92. * @return string
  93. */
  94. public function getRuleValidator()
  95. {
  96. $this->getRelatedClass('ruleValidatorClass', 'Business\\Validation', 'RuleValidator');
  97. return class_exists($this->ruleValidatorClass) ? new $this->ruleValidatorClass : null;
  98. }
  99. /**
  100. * Obtém instância da Model desta service
  101. * @return \Phalcony\Core\Model;
  102. */
  103. public function getModel()
  104. {
  105. $this->getRelatedClass('modelClass', 'Model');
  106. if (!class_exists($this->modelClass)) {
  107. $this->model = new Model;
  108. $modelClass = $this->modelClass;
  109. $arr = explode('\\', $modelClass);
  110. $modelAbsoluteName = array_pop($arr);
  111. $tableName = \Phalcon\Text::uncamelize($modelAbsoluteName);
  112. $this->model->setDataSource($tableName);
  113. }
  114. return $this->model = ($this->model ? $this->model : new $this->modelClass);
  115. }
  116. /**
  117. * Obtém instância da Repository desta service
  118. * @return \Phalcony\Core\Repository;
  119. */
  120. public function getRepository($model = null)
  121. {
  122. $this->getRelatedClass('repositoryClass', 'Repository');
  123. if (!class_exists($this->repositoryClass)) {
  124. $this->repository = new Repository($model);
  125. $this->repository->setModelClass($this->getRelatedClass('modelClass', 'Model'));
  126. }
  127. $this->repository = ($this->repository ? $this->repository : new $this->repositoryClass($model));
  128. if (!$this->repository->getDI()) {
  129. $this->repository->setDI($this->getDI());
  130. }
  131. return $this->repository;
  132. }
  133. /**
  134. * Obtém listagem com base em filtro
  135. *
  136. * @param integer $limit
  137. * @param integer $offset
  138. * @param array $filters
  139. * @param string $sortFields [description]
  140. * @param string $sortDirections [description]
  141. * @return array
  142. */
  143. public function getAll($limit, $offset, $filters = array(), $sortFields = 'id', $sortDirections = 'ASC')
  144. {
  145. $repo = $this->getRepository();
  146. return $repo->find($limit, $offset, $filters, $sortFields, $sortDirections);
  147. }
  148. /**
  149. * Contagem total com base nos filtros passados
  150. *
  151. * @return [type] [description]
  152. */
  153. public function countGetAll($filtros = array())
  154. {
  155. $repo = $this->getRepository();
  156. return $repo->countGetAll($filtros);
  157. }
  158. /**
  159. * Obtém os dados da pessoa utilizando um atributo
  160. *
  161. * @param [type] $id [description]
  162. * @return [type] [description]
  163. */
  164. public function getBy($attribute, $value, $model = null)
  165. {
  166. $repo = $this->getRepository();
  167. return $repo->getBy($attribute, $value, $model);
  168. }
  169. /**
  170. * Retorna a model de acordo com os dados fornecidos
  171. *
  172. * @param array $data [description]
  173. * @return Model [description]
  174. */
  175. public function getModelInstance($data = array())
  176. {
  177. $this->model = (isset($data['id']) && $data['id']) ?
  178. $this->getBy('id', $data['id']) :
  179. $this->getModel();
  180. return $this->model;
  181. }
  182. /**
  183. * Preenche a model de acordo com os dados fornecidos
  184. *
  185. * @param [type] $model [description]
  186. * @param [type] $data [description]
  187. * @return [type] [description]
  188. */
  189. public function fillModel($model, $data)
  190. {
  191. if (is_array($data)) {
  192. foreach ($data as $key => $value) {
  193. $model->$key = $value;
  194. }
  195. }
  196. }
  197. /**
  198. * Persiste os dados da entidade, tanto para insert quanto para update
  199. *
  200. * @param [type] $data [description]
  201. * @return [type] [description]
  202. */
  203. public function save($data)
  204. {
  205. $this->getModelInstance($data);
  206. $this->fillModel($this->model, $data);
  207. $this->getModelValidator() ? $this->getModelValidator()->saveValidation($this->model) : null;
  208. $this->getRuleValidator() ? $this->getRuleValidator()->saveValidation($this->model) : null;
  209. var_dump($this->getRepository()->getDI());
  210. $this->getRepository()->persistModel($this->model, $data);
  211. return $this->getModel();
  212. }
  213. /**
  214. * Exclusão física dos dados da entidade do banco
  215. *
  216. * @param [type] $id [description]
  217. * @return [type] [description]
  218. */
  219. public function delete($id)
  220. {
  221. $model = $this->getBy('id', $id);
  222. $this->getModelValidator() ? $this->getModelValidator()->deleteValidation($model) : null;
  223. $this->getRuleValidator() ? $this->getRuleValidator()->deleteValidation($model) : null;
  224. if (!$this->getRepository($model)->delete()) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. }