1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
6
|
|
|
use Illuminate\Contracts\Validation\Factory; |
7
|
|
|
use Illuminate\Foundation\Application; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
11
|
|
|
use Sco\Admin\Contracts\ConfigFactoryInterface; |
12
|
|
|
use Sco\Admin\Contracts\ModelFactoryInterface; |
13
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
14
|
|
|
|
15
|
|
|
class ModelFactory implements ModelFactoryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Illuminate\Foundation\Application |
19
|
|
|
*/ |
20
|
|
|
protected $app; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Sco\Admin\Contracts\Config |
24
|
|
|
*/ |
25
|
|
|
protected $configFactory; |
26
|
|
|
/** |
27
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
28
|
|
|
*/ |
29
|
|
|
//protected $model; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed|\Sco\Admin\Repositories\Repository |
33
|
|
|
*/ |
34
|
|
|
protected $repository; |
35
|
|
|
|
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
public function __construct(Application $app, ConfigFactoryInterface $factory) |
39
|
|
|
{ |
40
|
|
|
$this->app = $app; |
41
|
|
|
$this->configFactory = $factory; |
|
|
|
|
42
|
|
|
$this->config = new ConfigRepository( |
43
|
|
|
$this->getConfigValues() |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->repository = $this->app->make(RepositoryInterface::class); |
47
|
|
|
|
48
|
|
|
$this->repository->setClass( |
49
|
|
|
$this->config->get('class') |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getRepository() |
57
|
|
|
{ |
58
|
|
|
return $this->repository; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \Sco\Admin\Contracts\ConfigFactoryInterface |
63
|
|
|
*/ |
64
|
|
|
public function getConfigFactory() |
65
|
|
|
{ |
66
|
|
|
return $this->configFactory; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function get() |
70
|
|
|
{ |
71
|
|
|
$repository = $this->getRepository(); |
72
|
|
|
$orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']); |
73
|
|
|
$query = $repository->orderBy($orderBy[0], $orderBy[1]); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ($repository->isRestorable()) { |
76
|
|
|
$query = $query->withTrashed(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->usePagination()) { |
80
|
|
|
$data = $query->paginate($this->config->get('perPage')); |
81
|
|
|
|
82
|
|
|
$data->setCollection($this->parseRows($data->items())); |
83
|
|
|
} else { |
84
|
|
|
$data = $this->parseRows($query->get()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function store() |
91
|
|
|
{ |
92
|
|
|
$this->validate(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function update() |
96
|
|
|
{ |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function delete($id) |
100
|
|
|
{ |
101
|
|
|
$this->getRepository()->findOrFail($id)->delete(); |
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function forceDelete($id) |
106
|
|
|
{ |
107
|
|
|
$this->getRepository()->forceDelete($id); |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function restore($id) |
112
|
|
|
{ |
113
|
|
|
$this->getRepository()->restore($id); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function parseRows($rows) |
117
|
|
|
{ |
118
|
|
|
$data = collect(); |
119
|
|
|
if ($rows) { |
120
|
|
|
foreach ($rows as $row) { |
121
|
|
|
$newRow = collect(); |
122
|
|
|
foreach ($this->configFactory->getColumns() as $column) { |
123
|
|
|
$newRow->put($column->getName(), $column->setModel($row)->render()); |
124
|
|
|
|
125
|
|
|
// whether this row has been soft deleted |
126
|
|
|
if ($this->getRepository()->isRestorable()) { |
127
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$data->push($newRow); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return $data; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function usePagination() |
137
|
|
|
{ |
138
|
|
|
return $this->config->get('perPage') > 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getConfigValues() |
142
|
|
|
{ |
143
|
|
|
$config = $this->configFactory->getConfigRepository()->get('model'); |
144
|
|
|
if (is_string($config)) { |
145
|
|
|
$config = [ |
146
|
|
|
'class' => $config |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
$config = array_merge([ |
150
|
|
|
//'orderBy' => [], |
|
|
|
|
151
|
|
|
'perPage' => 10, |
152
|
|
|
], $config); |
153
|
|
|
return $config; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function validate() |
157
|
|
|
{ |
158
|
|
|
$this->app->make(Factory::class)->validate($data, $rules, $messages, $customAttributes); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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..