1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Repositories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
8
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
9
|
|
|
use Sco\Admin\Exceptions\RepositoryException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method static \Illuminate\Database\Eloquent\Model getKeyName() |
13
|
|
|
*/ |
14
|
|
|
class Repository implements RepositoryInterface |
15
|
|
|
{ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
protected $class; |
19
|
|
|
|
20
|
|
|
public function getModel() |
21
|
|
|
{ |
22
|
|
|
return $this->model; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setModel(Model $model) |
26
|
|
|
{ |
27
|
|
|
$this->model = $model; |
28
|
|
|
$this->class = get_class($model); |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getClass() |
34
|
|
|
{ |
35
|
|
|
return $this->class; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setClass($class) |
39
|
|
|
{ |
40
|
|
|
if (!class_exists($class)) { |
41
|
|
|
throw new RepositoryException("Class {$class} not found."); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->class = $class; |
45
|
|
|
$this->setModel( |
46
|
|
|
new $class() |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $id |
54
|
|
|
* |
55
|
|
|
* @return Model |
56
|
|
|
*/ |
57
|
|
|
public function findOnlyTrashed($id) |
58
|
|
|
{ |
59
|
|
|
return $this->getModel()->onlyTrashed()->findOrFail($id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function store() |
64
|
|
|
{ |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function update() |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function forceDelete($id) |
76
|
|
|
{ |
77
|
|
|
return $this->findOnlyTrashed($id)->forceDelete(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function restore($id) |
81
|
|
|
{ |
82
|
|
|
return $this->findOnlyTrashed($id)->restore(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
public function isRestorable() |
87
|
|
|
{ |
88
|
|
|
return in_array(SoftDeletes::class, class_uses_recursive($this->getClass())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Handle dynamic method calls into the model. |
93
|
|
|
* |
94
|
|
|
* @param string $method |
95
|
|
|
* @param array $parameters |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function __call($method, $parameters) |
100
|
|
|
{ |
101
|
|
|
return $this->getModel()->$method(...$parameters); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Handle dynamic static method calls into the method. |
106
|
|
|
* |
107
|
|
|
* @param string $method |
108
|
|
|
* @param array $parameters |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public static function __callStatic($method, $parameters) |
113
|
|
|
{ |
114
|
|
|
return (new static)->$method(...$parameters); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|