1
|
|
|
<?php namespace Usman\Guardian\Repositories; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
use Usman\Guardian\Repositories\Interfaces\BaseRepositoryInterface; |
5
|
|
|
|
6
|
|
|
abstract class BaseRepository implements BaseRepositoryInterface { |
7
|
|
|
|
8
|
|
|
const PAGE = 20; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Model Instance |
12
|
|
|
* |
13
|
|
|
* @var Model |
14
|
|
|
*/ |
15
|
|
|
protected $model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates a new repository |
19
|
|
|
* |
20
|
|
|
* @param Illuminate\Database\Eloquent\Model $model |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Model $model) |
23
|
|
|
{ |
24
|
|
|
$this->model = $model; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Finds a single record by given id. |
29
|
|
|
* |
30
|
|
|
* @param int $id |
31
|
|
|
* @return Illuminate\Database\Eloquent\Model |
32
|
|
|
*/ |
33
|
|
|
public function findById($id) |
34
|
|
|
{ |
35
|
|
|
return $this->model->findOrFail($id); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieves a single record from storage with related records |
40
|
|
|
* |
41
|
|
|
* @param int $id |
42
|
|
|
* @param string $related |
43
|
|
|
* @return Illuminate\Database\Eloquent\Model |
44
|
|
|
*/ |
45
|
|
|
public function findByIdWith($id, $related) |
46
|
|
|
{ |
47
|
|
|
return $this->model->with($related)->findOrFail($id); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retrieves a single page of records with related records |
52
|
|
|
* |
53
|
|
|
* @param string $related |
54
|
|
|
* @param int $perPage |
55
|
|
|
* @return Illuminate\Pagination\Paginator an instance of paginator. |
56
|
|
|
*/ |
57
|
|
|
public function getbyPageWith($related, $perPage = self::PAGE) |
58
|
|
|
{ |
59
|
|
|
return $this->model->with($related)->paginate($perPage); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieves all records from storage |
64
|
|
|
* |
65
|
|
|
* @param array $col |
66
|
|
|
* @return Illuminate\Support\Collection |
67
|
|
|
*/ |
68
|
|
|
public function getAll($col = ['*']) |
69
|
|
|
{ |
70
|
|
|
return $this->model->get($col); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Template method for creating a record in storage |
75
|
|
|
* |
76
|
|
|
* @param array $fields |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function create(array $fields) |
80
|
|
|
{ |
81
|
|
|
$this->fillData($fields); |
82
|
|
|
return $this->model->save() ? $this->model->id : false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Template method for updating a record in storage |
87
|
|
|
* |
88
|
|
|
* @param int $id |
89
|
|
|
* @param array $fields |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function update($id, array $fields) |
93
|
|
|
{ |
94
|
|
|
//caching the updated model |
95
|
|
|
$this->model = $this->findById($id); |
|
|
|
|
96
|
|
|
$this->fillData($fields); |
97
|
|
|
return $this->model->save($fields); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Deletes a record from storage |
102
|
|
|
* |
103
|
|
|
* @param int $id |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function delete($id) |
107
|
|
|
{ |
108
|
|
|
return $this->findById($id)->delete(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Deletes a record and its related records. |
113
|
|
|
* |
114
|
|
|
* @param int $id |
115
|
|
|
* @param array $method methods that define the relation |
|
|
|
|
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function deleteWith($id, array $methods) |
119
|
|
|
{ |
120
|
|
|
$model = $this->findById($id); |
121
|
|
|
|
122
|
|
|
foreach($methods as $method) |
123
|
|
|
{ |
124
|
|
|
$model->{$method}()->detach(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $model->delete(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Attaches the related ids in pivot table |
132
|
|
|
* |
133
|
|
|
* @param ind $id |
134
|
|
|
* @param array $ids |
135
|
|
|
* @param string $method Name of the method that defines the relation |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function attach($id, array $ids = [], $method) |
139
|
|
|
{ |
140
|
|
|
//checking if this id is set on a recently updated or created model. |
141
|
|
|
if($this->model->id == $id) |
142
|
|
|
{ |
143
|
|
|
$this->model->{$method}()->withTimeStamps()->sync($ids); |
144
|
|
|
} |
145
|
|
|
else |
146
|
|
|
{ |
147
|
|
|
$this->findById($id)->{$method}()->withTimeStamps()->sync($ids); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Fills the model attributes. |
153
|
|
|
* |
154
|
|
|
* @param array $data |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
abstract protected function fillData(array $data); |
158
|
|
|
|
159
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..