|
1
|
|
|
<?php |
|
2
|
|
|
namespace Afrittella\BackProject\Repositories; |
|
3
|
|
|
|
|
4
|
|
|
use Afrittella\BackProject\Contracts\BaseRepository; |
|
5
|
|
|
use Afrittella\BackProject\Contracts\CriteriaInterface; |
|
6
|
|
|
use Afrittella\BackProject\Exceptions\NotFoundException; |
|
7
|
|
|
use Afrittella\BackProject\Exceptions\NotSavedException; |
|
8
|
|
|
use Afrittella\BackProject\Exceptions\RepositoryException; |
|
9
|
|
|
use Afrittella\BackProject\Repositories\Criteria\Criteria; |
|
10
|
|
|
use Illuminate\Container\Container as App; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
/*** |
|
15
|
|
|
* Class Base |
|
16
|
|
|
* Freely inspired by https://github.com/bosnadev/repository |
|
17
|
|
|
* @package Afrittella\BackProject\Repositories |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
abstract class Base implements BaseRepository, CriteriaInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private $app; |
|
23
|
|
|
|
|
24
|
|
|
protected $model; |
|
25
|
|
|
|
|
26
|
|
|
protected $criteria; |
|
27
|
|
|
|
|
28
|
|
|
protected $skipCriteria = false; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(App $app, Collection $collection) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->app = $app; |
|
33
|
|
|
$this->criteria = $collection; |
|
34
|
|
|
$this->resetScope(); |
|
35
|
|
|
$this->makeModel(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
abstract public function model(); |
|
39
|
|
|
|
|
40
|
|
|
public function all($columns = ['*']) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->applyCriteria(); |
|
43
|
|
|
return $this->model->get($columns); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function paginate($perPage = 15, $columns = array('*')) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->applyCriteria(); |
|
49
|
|
|
return $this->model->paginate($perPage, $columns); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function create(array $data) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->model->create($data); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function update(array $data, $id, $attribute="id") |
|
58
|
|
|
{ |
|
59
|
|
|
$model_data = $this->model->where($attribute, '=', $id)->first(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$model_data) { |
|
62
|
|
|
throw new NotFoundException(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!$model_data->update($data)) { |
|
66
|
|
|
throw new NotSavedException(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function delete($id, $attribute = 'id') |
|
73
|
|
|
{ |
|
74
|
|
|
$model_data = $this->model->where($attribute, '=', $id)->first(); |
|
75
|
|
|
|
|
76
|
|
|
if (!$model_data) { |
|
77
|
|
|
throw new NotFoundException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!$model_data->destroy($id)) { |
|
81
|
|
|
throw new NotDeletexception(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function find($id, $columns = array('*')) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->applyCriteria(); |
|
90
|
|
|
|
|
91
|
|
|
$model_data = $this->model->find($id, $columns); |
|
92
|
|
|
|
|
93
|
|
|
if (!$model_data) { |
|
94
|
|
|
throw new NotFoundException(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $model_data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function firstOrCreate($data) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->model->findOrCreate($data); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function findBy($attribute, $value, $columns = array('*')) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->applyCriteria(); |
|
108
|
|
|
return $this->model->where($attribute, '=', $value)->first($columns); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function findAllBy($field, $value, $columns = array('*')) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->applyCriteria(); |
|
114
|
|
|
return $this->model->where($field, '=', $value)->get($columns); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function findWhere($where, $columns = array('*'), $or = false) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->applyCriteria(); |
|
120
|
|
|
|
|
121
|
|
|
$model = $this->model; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($where as $field => $value) { |
|
124
|
|
|
if ($value instanceof \Closure) { |
|
125
|
|
|
$model = (!$or) |
|
126
|
|
|
? $model->where($value) |
|
127
|
|
|
: $model->orWhere($value); |
|
128
|
|
|
} elseif (is_array($value)) { |
|
129
|
|
|
if (count($value) === 3) { |
|
130
|
|
|
list($field, $operator, $search) = $value; |
|
131
|
|
|
$model = (!$or) |
|
132
|
|
|
? $model->where($field, $operator, $search) |
|
133
|
|
|
: $model->orWhere($field, $operator, $search); |
|
134
|
|
|
} elseif (count($value) === 2) { |
|
135
|
|
|
list($field, $search) = $value; |
|
136
|
|
|
$model = (!$or) |
|
137
|
|
|
? $model->where($field, '=', $search) |
|
138
|
|
|
: $model->orWhere($field, '=', $search); |
|
139
|
|
|
} |
|
140
|
|
|
} else { |
|
141
|
|
|
$model = (!$or) |
|
142
|
|
|
? $model->where($field, '=', $value) |
|
143
|
|
|
: $model->orWhere($field, '=', $value); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
return $model->get($columns); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
public function makeModel() |
|
151
|
|
|
{ |
|
152
|
|
|
$model = $this->app->make($this->model()); |
|
153
|
|
|
//$model = app()->make($this->model()); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
if (!$model instanceof Model) |
|
156
|
|
|
throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"); |
|
157
|
|
|
|
|
158
|
|
|
return $this->model = $model; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getModel() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->model; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function resetScope() { |
|
167
|
|
|
$this->skipCriteria(false); |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function skipCriteria($status = true){ |
|
172
|
|
|
$this->skipCriteria = $status; |
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getCriteria() { |
|
177
|
|
|
return $this->criteria; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function getByCriteria(Criteria $criteria) { |
|
181
|
|
|
$this->model = $criteria->apply($this->model, $this); |
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function pushCriteria(Criteria $criteria) { |
|
186
|
|
|
$this->criteria->push($criteria); |
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function applyCriteria() { |
|
191
|
|
|
if($this->skipCriteria === true) |
|
192
|
|
|
return $this; |
|
193
|
|
|
|
|
194
|
|
|
foreach($this->getCriteria() as $criteria) { |
|
195
|
|
|
if($criteria instanceof Criteria) |
|
196
|
|
|
$this->model = $criteria->apply($this->model, $this); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.