1 | <?php |
||
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) |
||
49 | |||
50 | /** |
||
51 | * Get All records |
||
52 | * |
||
53 | * @return Illuminate\Database\Eloquent\Collection |
||
54 | */ |
||
55 | public function getAll() |
||
59 | |||
60 | /** |
||
61 | * Get record by ID |
||
62 | * |
||
63 | * @param int $id |
||
64 | * @return Illuminate\Database\Eloquent\Model |
||
65 | */ |
||
66 | public function getById($id) |
||
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) |
||
97 | |||
98 | /** |
||
99 | * Paginate the results |
||
100 | * |
||
101 | * @param int $paginated |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function paginate($paginated = 25) |
||
110 | |||
111 | /** |
||
112 | * Rebuild the current url query string for the pagination links |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function buildUrlQuery() |
||
125 | |||
126 | /** |
||
127 | * Reset the Model to its original form |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function reset() |
||
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) |
||
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) |
||
167 | |||
168 | } |
||
169 |