1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Repositories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
8
|
|
|
use Illuminate\Foundation\Application; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
11
|
|
|
use Sco\Admin\Contracts\WithModel; |
12
|
|
|
|
13
|
|
|
class Repository implements RepositoryInterface |
14
|
|
|
{ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
protected $model; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Model |
21
|
|
|
*/ |
22
|
|
|
protected $class; |
23
|
|
|
|
24
|
|
|
protected $with = []; |
25
|
|
|
|
26
|
|
|
public function __construct(Application $app) |
27
|
|
|
{ |
28
|
|
|
$this->app = $app; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getModel() |
32
|
|
|
{ |
33
|
|
|
return $this->model; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setModel(Model $model) |
42
|
|
|
{ |
43
|
|
|
$this->model = $model; |
44
|
|
|
$this->class = get_class($model); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string[] |
51
|
|
|
*/ |
52
|
|
|
public function getWith() |
53
|
|
|
{ |
54
|
|
|
return $this->with; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function with($relations) |
58
|
|
|
{ |
59
|
|
|
$this->with = array_flatten(func_get_args()); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getClass() |
65
|
|
|
{ |
66
|
|
|
return $this->class; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setClass($class) |
70
|
|
|
{ |
71
|
|
|
if (!class_exists($class)) { |
72
|
|
|
throw new InvalidArgumentException("Model class {$class} not found."); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->class = $class; |
76
|
|
|
$this->setModel( |
77
|
|
|
new $class() |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getQuery() |
87
|
|
|
{ |
88
|
|
|
$model = $this->getModel(); |
89
|
|
|
|
90
|
|
|
return $model->query()->with($this->getWith()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function find($id) |
94
|
|
|
{ |
95
|
|
|
$query = $this->getQuery(); |
96
|
|
|
if ($this->isRestorable()) { |
97
|
|
|
$query->withTrashed(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $query->find($id); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function findOrFail($id) |
104
|
|
|
{ |
105
|
|
|
$query = $this->getQuery(); |
106
|
|
|
if ($this->isRestorable()) { |
107
|
|
|
$query->withTrashed(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $query->findOrFail($id); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function findOnlyTrashed($id) |
117
|
|
|
{ |
118
|
|
|
return $this->getQuery()->onlyTrashed()->findOrFail($id); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function delete($id) |
122
|
|
|
{ |
123
|
|
|
return $this->findOrFail($id)->delete(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function forceDelete($id) |
127
|
|
|
{ |
128
|
|
|
return $this->findOnlyTrashed($id)->forceDelete(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function restore($id) |
132
|
|
|
{ |
133
|
|
|
return $this->findOnlyTrashed($id)->restore(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function isRestorable() |
137
|
|
|
{ |
138
|
|
|
return in_array(SoftDeletes::class, class_uses_recursive($this->getClass())); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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..