1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwoDojo\ModuleManager\Repositories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use TwoDojo\ModuleManager\Contracts\Repository; |
7
|
|
|
|
8
|
|
|
class EloquentRepository implements Repository |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
12
|
|
|
*/ |
13
|
|
|
protected $model; |
14
|
|
|
|
15
|
|
|
protected $scopes = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* EloquentRepository constructor. |
19
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $model |
20
|
|
|
*/ |
21
|
|
|
public function __construct($model = null) |
22
|
|
|
{ |
23
|
|
|
if ($model !== null) { |
24
|
|
|
$this->setModel($model); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set the repository model. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
32
|
|
|
*/ |
33
|
|
|
public function setModel(Model $model) |
34
|
|
|
{ |
35
|
|
|
$this->model = $model; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the repository model. |
40
|
|
|
* |
41
|
|
|
* @return Model |
42
|
|
|
*/ |
43
|
|
|
public function getModel() |
44
|
|
|
{ |
45
|
|
|
return $this->model; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function save(array $attributes) |
52
|
|
|
{ |
53
|
|
|
$model = app()->make(get_class($this->model)); |
54
|
|
|
$model->fill($attributes); |
55
|
|
|
$model->save(); |
56
|
|
|
|
57
|
|
|
return $model; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function update($id, array $attributes) |
64
|
|
|
{ |
65
|
|
|
$model = $id; |
66
|
|
|
if (!($id instanceof Model)) { |
67
|
|
|
$model = $this->find($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $model->update($attributes); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function find($id) |
77
|
|
|
{ |
78
|
|
|
return $this->applyScopes()->find($id); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function findByField(string $field, $value) |
85
|
|
|
{ |
86
|
|
|
return $this->applyScopes()->where($field, $value)->get(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function all() |
93
|
|
|
{ |
94
|
|
|
return $this->applyScopes()->get(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
99
|
|
|
*/ |
100
|
|
|
protected function applyScopes() |
101
|
|
|
{ |
102
|
|
|
$model = $this->model; |
103
|
|
|
|
104
|
|
|
foreach ($this->scopes as $record) { |
105
|
|
|
list($scope, $arguments) = $record; |
106
|
|
|
$model = $model->{$scope}(...$arguments); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->scopes = []; |
110
|
|
|
|
111
|
|
|
return $model; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $scope |
116
|
|
|
* @param $arguments |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function __call($scope, $arguments) |
120
|
|
|
{ |
121
|
|
|
$this->scopes[] = [$scope, $arguments]; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|