1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Repo; |
4
|
|
|
|
5
|
|
|
use \BadMethodCallException; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Facades\Input; |
8
|
|
|
|
9
|
|
|
abstract class BaseRepository { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Model instance |
13
|
|
|
* |
14
|
|
|
* @var Model |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Original Model instance |
20
|
|
|
* |
21
|
|
|
* @var Model |
22
|
|
|
*/ |
23
|
|
|
protected $originalModel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Allowed Model Methods |
27
|
|
|
* |
28
|
|
|
* Whitelist of methods that can be invoked publicly by the repository on the model |
29
|
|
|
* These should be methods that won't break the model's integration in the repository. |
30
|
|
|
* e.g. fetching the latest 5 rows: $repo->limit(5)->orderBy('created_at','DESC')->getAll(); |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $allowedModelMethods = [ |
35
|
|
|
'orderBy', 'limit', 'offset', 'take', 'skip', 'with' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Paginate our result set |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $paginated = null; |
44
|
|
|
|
45
|
|
|
public function __construct(Model $model) |
46
|
|
|
{ |
47
|
|
|
$this->setModel($model); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get All records |
52
|
|
|
* |
53
|
|
|
* @return Illuminate\Database\Eloquent\Collection |
54
|
|
|
*/ |
55
|
|
|
public function getAll() |
56
|
|
|
{ |
57
|
|
|
return $this->fetch(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get record by ID |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return Illuminate\Database\Eloquent\Model |
65
|
|
|
*/ |
66
|
|
|
public function getById($id) |
67
|
|
|
{ |
68
|
|
|
return $this->model->where($this->originalModel->getTable() . '.id', $id)->first(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Base fetch for all repository returns |
73
|
|
|
* This should be used when returning multiple records |
74
|
|
|
* |
75
|
|
|
* @param Model $model - update model instance |
76
|
|
|
* @return Illuminate\Database\Eloquent\Collection |
77
|
|
|
*/ |
78
|
|
|
public function fetch($model = null) |
79
|
|
|
{ |
80
|
|
|
if ( !is_null($model) ) |
81
|
|
|
{ |
82
|
|
|
$this->model = $model; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$result = ($this->paginated && is_int($this->paginated)) ? $this->model->paginate($this->paginated) : $this->model->get(); |
86
|
|
|
|
87
|
|
|
if ( $this->paginated ) |
88
|
|
|
{ |
89
|
|
|
// Append custom query string to page links to maintain correct url filter and sorting |
90
|
|
|
$result = $result->appends($this->buildUrlQuery()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->reset(); |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Paginate the results |
100
|
|
|
* |
101
|
|
|
* @param int $paginated |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function paginate($paginated = 25) |
105
|
|
|
{ |
106
|
|
|
$this->paginated = (int)$paginated; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Rebuild the current url query string for the pagination links |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function buildUrlQuery() |
117
|
|
|
{ |
118
|
|
|
$query = array_map(function($value){ return is_string($value) ? urlencode($value) : $value; },Input::query()); |
119
|
|
|
|
120
|
|
|
// Remove 'own' page query element |
121
|
|
|
if ( isset($query['page']) ) unset($query['page']); |
122
|
|
|
|
123
|
|
|
return $query; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Reset the Model to its original form |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
protected function reset() |
132
|
|
|
{ |
133
|
|
|
$this->paginated = null; |
134
|
|
|
if ( !is_null($this->originalModel) ) $this->setModel($this->originalModel); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Assign a model to our Repository |
139
|
|
|
* This method must be called by the child class constructor at instantiating of the class |
140
|
|
|
* |
141
|
|
|
* @param Model $model |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected function setModel(Model $model) |
145
|
|
|
{ |
146
|
|
|
$this->model = $this->originalModel = $model; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Call to Eloquent model method |
151
|
|
|
* |
152
|
|
|
* @param $method |
153
|
|
|
* @param $parameters |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function __call($method, $parameters) |
157
|
|
|
{ |
158
|
|
|
if ( in_array($method, $this->allowedModelMethods) ) |
159
|
|
|
{ |
160
|
|
|
$this->model = call_user_func_array([$this->model, $method], $parameters); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
throw new BadMethodCallException('Method [' . $method . '] does not exist on the [' . get_class($this) . '] repository or its model instance: [' . get_class($this->model) . ']'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|