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