Repository.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace App\Repositories\Base;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Container\Container as App;
  6. use App\Repositories\Contracts\RepositoryInterface;
  7. use App\Repositories\Exceptions\RepositoryException;
  8. use App\Repositories\Contracts\CriteriaInterface;
  9. use App\Repositories\Base\Criteria;
  10. /**
  11. * Class Repository
  12. * @package Bosnadev\Repositories\Eloquent
  13. */
  14. abstract class Repository implements RepositoryInterface, CriteriaInterface
  15. {
  16. /**
  17. * @var App
  18. */
  19. private $app;
  20. /**
  21. * @var Model
  22. */
  23. protected $model;
  24. protected $newModel;
  25. /**
  26. * @var Collection
  27. */
  28. protected $criteria;
  29. /**
  30. * @var bool
  31. */
  32. protected $skipCriteria = false;
  33. /**
  34. * Prevents from overwriting same criteria in chain usage
  35. * @var bool
  36. */
  37. protected $preventCriteriaOverwriting = true;
  38. /**
  39. * @param App $app
  40. * @param Collection $collection
  41. * @throws Exceptions\RepositoryException
  42. */
  43. public function __construct(App $app, Collection $collection)
  44. {
  45. $this->app = $app;
  46. $this->criteria = $collection;
  47. $this->resetScope();
  48. $this->makeModel();
  49. }
  50. /**
  51. * Specify Model class name
  52. *
  53. * @return mixed
  54. */
  55. public abstract function model();
  56. /**
  57. * @param array $columns
  58. * @return mixed
  59. */
  60. public function all($columns = array('*'))
  61. {
  62. $this->applyCriteria();
  63. return $this->model->get($columns);
  64. }
  65. /**
  66. * @param array $relations
  67. * @return $this
  68. */
  69. public function with(array $relations)
  70. {
  71. $this->model = $this->model->with($relations);
  72. return $this;
  73. }
  74. /**
  75. * @param string $value
  76. * @param string $key
  77. * @return array
  78. */
  79. public function lists($value, $key = null)
  80. {
  81. $this->applyCriteria();
  82. $lists = $this->model->lists($value, $key);
  83. if (is_array($lists)) {
  84. return $lists;
  85. }
  86. return $lists->all();
  87. }
  88. /**
  89. * @param int $perPage
  90. * @param array $columns
  91. * @return mixed
  92. */
  93. public function paginate($perPage = 25, $columns = array('*'))
  94. {
  95. $this->applyCriteria();
  96. return $this->model->paginate($perPage, $columns);
  97. }
  98. /**
  99. * @param array $data
  100. * @return mixed
  101. */
  102. public function create(array $data)
  103. {
  104. return $this->model->create($data);
  105. }
  106. /**
  107. * save a model without massive assignment
  108. *
  109. * @param array $data
  110. * @return bool
  111. */
  112. public function saveModel(array $data)
  113. {
  114. foreach ($data as $k => $v) {
  115. $this->model->$k = $v;
  116. }
  117. return $this->model->save();
  118. }
  119. /**
  120. * @param array $data
  121. * @param $id
  122. * @param string $attribute
  123. * @return mixed
  124. */
  125. public function update($id,array $data, $attribute = "id")
  126. {
  127. return $this->model->where($attribute, '=', $id)->update($data);
  128. }
  129. /**
  130. * @param array $data
  131. * @param $id
  132. * @return mixed
  133. */
  134. public function updateRich( $id,array $data)
  135. {
  136. if (!($model = $this->model->find($id))) {
  137. return false;
  138. }
  139. return $model->fill($data)->save();
  140. }
  141. /**
  142. * @param $id
  143. * @return mixed
  144. */
  145. public function delete($id)
  146. {
  147. return $this->model->destroy($id);
  148. }
  149. /**
  150. * @param $id
  151. * @return mixed
  152. */
  153. public function destroy($id)
  154. {
  155. $data = $this->model->find($id);
  156. if ( $data->delete()) {
  157. return true;
  158. }else
  159. return false;
  160. }
  161. /**
  162. * @param $id
  163. * @param array $columns
  164. * @return mixed
  165. */
  166. public function find($id, $columns = array('*'))
  167. {
  168. $this->applyCriteria();
  169. return $this->model->find($id, $columns);
  170. }
  171. /**
  172. * @param $attribute
  173. * @param $value
  174. * @param array $columns
  175. * @return mixed
  176. */
  177. public function findBy($attribute, $value, $columns = array('*'))
  178. {
  179. $this->applyCriteria();
  180. return $this->model->where($attribute, '=', $value)->first($columns);
  181. }
  182. /**
  183. * @param $attribute
  184. * @param $value
  185. * @param array $columns
  186. * @return mixed
  187. */
  188. public function findAllBy($attribute, $value, $columns = array('*'))
  189. {
  190. $this->applyCriteria();
  191. return $this->model->where($attribute, '=', $value)->get($columns);
  192. }
  193. /**
  194. * Find a collection of models by the given query conditions.
  195. *
  196. * @param array $where
  197. * @param array $columns
  198. * @param bool $or
  199. *
  200. * @return \Illuminate\Database\Eloquent\Collection|null
  201. */
  202. public function findWhere($where, $columns = ['*'], $or = false)
  203. {
  204. $this->applyCriteria();
  205. $model = $this->model;
  206. foreach ($where as $field => $value) {
  207. if ($value instanceof \Closure) {
  208. $model = (!$or)
  209. ? $model->where($value)
  210. : $model->orWhere($value);
  211. } elseif (is_array($value)) {
  212. if (count($value) === 3) {
  213. list($field, $operator, $search) = $value;
  214. $model = (!$or)
  215. ? $model->where($field, $operator, $search)
  216. : $model->orWhere($field, $operator, $search);
  217. } elseif (count($value) === 2) {
  218. list($field, $search) = $value;
  219. $model = (!$or)
  220. ? $model->where($field, '=', $search)
  221. : $model->orWhere($field, '=', $search);
  222. }
  223. } else {
  224. $model = (!$or)
  225. ? $model->where($field, '=', $value)
  226. : $model->orWhere($field, '=', $value);
  227. }
  228. }
  229. return $model->get($columns);
  230. }
  231. /**
  232. * @return \Illuminate\Database\Eloquent\Builder
  233. * @throws RepositoryException
  234. */
  235. public function makeModel()
  236. {
  237. return $this->setModel($this->model());
  238. }
  239. /**
  240. * Set Eloquent Model to instantiate
  241. *
  242. * @param $eloquentModel
  243. * @return Model
  244. * @throws RepositoryException
  245. */
  246. public function setModel($eloquentModel)
  247. {
  248. $this->newModel = $this->app->make($eloquentModel);
  249. if (!$this->newModel instanceof Model)
  250. throw new RepositoryException("Class {$this->newModel} must be an instance of Illuminate\\Database\\Eloquent\\Model");
  251. return $this->model = $this->newModel;
  252. }
  253. /**
  254. * @return $this
  255. */
  256. public function resetScope()
  257. {
  258. $this->skipCriteria(false);
  259. return $this;
  260. }
  261. /**
  262. * @param bool $status
  263. * @return $this
  264. */
  265. public function skipCriteria($status = true)
  266. {
  267. $this->skipCriteria = $status;
  268. return $this;
  269. }
  270. /**
  271. * @return mixed
  272. */
  273. public function getCriteria()
  274. {
  275. return $this->criteria;
  276. }
  277. /**
  278. * @param Criteria $criteria
  279. * @return $this
  280. */
  281. public function getByCriteria(Criteria $criteria)
  282. {
  283. $this->model = $criteria->apply($this->model, $this);
  284. return $this;
  285. }
  286. /**
  287. * @param Criteria $criteria
  288. * @return $this
  289. */
  290. public function pushCriteria(Criteria $criteria)
  291. {
  292. if ($this->preventCriteriaOverwriting) {
  293. // Find existing criteria
  294. $key = $this->criteria->search(function ($item) use ($criteria) {
  295. return (is_object($item) && (get_class($item) == get_class($criteria)));
  296. });
  297. // Remove old criteria
  298. if (is_int($key)) {
  299. $this->criteria->offsetUnset($key);
  300. }
  301. }
  302. $this->criteria->push($criteria);
  303. return $this;
  304. }
  305. /**
  306. * @return $this
  307. */
  308. public function applyCriteria()
  309. {
  310. if ($this->skipCriteria === true)
  311. return $this;
  312. foreach ($this->getCriteria() as $criteria) {
  313. if ($criteria instanceof Criteria)
  314. $this->model = $criteria->apply($this->model, $this);
  315. }
  316. return $this;
  317. }
  318. }