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
|
|
|
public function setModel(Model $model) |
37
|
|
|
{ |
38
|
|
|
$this->model = $model; |
39
|
|
|
$this->class = get_class($model); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string[] |
46
|
|
|
*/ |
47
|
|
|
public function getWith() |
48
|
|
|
{ |
49
|
|
|
return $this->with; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function with($relations) |
53
|
|
|
{ |
54
|
|
|
$this->with = array_flatten(func_get_args()); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getClass() |
60
|
|
|
{ |
61
|
|
|
return $this->class; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setClass($class) |
65
|
|
|
{ |
66
|
|
|
if (!class_exists($class)) { |
67
|
|
|
throw new InvalidArgumentException("Model class {$class} not found."); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->class = $class; |
71
|
|
|
$this->setModel( |
72
|
|
|
new $class() |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getQuery() |
82
|
|
|
{ |
83
|
|
|
$model = $this->getModel(); |
84
|
|
|
|
85
|
|
|
return $model->query()->with($this->getWith()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function find($id) |
89
|
|
|
{ |
90
|
|
|
$query = $this->getQuery(); |
91
|
|
|
if ($this->isRestorable()) { |
92
|
|
|
$query->withTrashed(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $query->find($id); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function findOrFail($id) |
99
|
|
|
{ |
100
|
|
|
$query = $this->getQuery(); |
101
|
|
|
if ($this->isRestorable()) { |
102
|
|
|
$query->withTrashed(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $query->findOrFail($id); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function findOnlyTrashed($id) |
112
|
|
|
{ |
113
|
|
|
return $this->getQuery()->onlyTrashed()->findOrFail($id); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/*public function store() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function update() |
121
|
|
|
{ |
122
|
|
|
}*/ |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
public function forceDelete($id) |
126
|
|
|
{ |
127
|
|
|
return $this->findOnlyTrashed($id)->forceDelete(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function restore($id) |
131
|
|
|
{ |
132
|
|
|
return $this->findOnlyTrashed($id)->restore(); |
133
|
|
|
} |
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..