-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRepository.php
More file actions
executable file
·248 lines (213 loc) · 5.54 KB
/
Copy pathRepository.php
File metadata and controls
executable file
·248 lines (213 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace ModStart\Repository;
use Doctrine\DBAL\Query\QueryBuilder;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use ModStart\Detail\Detail;
use ModStart\Form\Form;
use ModStart\Grid\Model;
use ModStart\Support\Concern\HasArguments;
abstract class Repository implements RepositoryInterface, SortRepositoryInterface, TreeRepositoryInterface
{
use Macroable;
use HasArguments;
/**
* @var string
*/
protected $keyName = 'id';
/**
* @var bool
*/
protected $isSoftDeletes = false;
/**
* 获取主键名称.
*
* @return string
*/
public function getKeyName()
{
return $this->keyName ? $this->keyName : 'id';
}
/**
* 设置主键名称.
*
* @param string $keyName
*/
public function setKeyName($keyName)
{
$this->keyName = $keyName;
}
/**
* 获取创建时间字段.
*
* @return string
*/
public function getCreatedAtColumn()
{
return 'created_at';
}
/**
* 获取更新时间字段.
*
* @return string
*/
public function getUpdatedAtColumn()
{
return 'updated_at';
}
/**
* 是否使用软删除.
*
* @return bool
*/
public function isSoftDeletes()
{
return $this->isSoftDeletes;
}
/**
* @param bool $isSoftDeletes
*/
public function setIsSoftDeletes($isSoftDeletes)
{
$this->isSoftDeletes = $isSoftDeletes;
}
public function add(Form $form)
{
throw new \RuntimeException('This repository does not support "add" method');
}
public function editing(Form $form)
{
throw new \RuntimeException('This repository does not support "editing" method');
}
public function edit(Form $form)
{
throw new \RuntimeException('This repository does not support "edit" method');
}
public function show(Detail $detail)
{
throw new \RuntimeException('This repository does not support "show" method');
}
public function deleting(Form $form)
{
throw new \RuntimeException('This repository does not support "delete" method');
}
public function delete(Form $form, Arrayable $deletingData)
{
throw new \RuntimeException('This repository does not support "delete" method');
}
/**
* list items for conditions
* @param Model $model
* @return Collection
*/
public function get(Model $model)
{
throw new \RuntimeException('This repository does not support "get" method');
}
/**
* get built model query for conditions
* @param Model $model
* @return QueryBuilder
*/
public function getQuery(Model $model)
{
throw new \RuntimeException('This repository dose not support "getQuery" method');
}
private $sortColumn = 'sort';
/**
* 获取排序字段
* @return string
*/
public function getSortColumn()
{
return $this->sortColumn;
}
/**
* 设置排序字段
* @param $value
*/
public function setSortColumn($value)
{
$this->sortColumn = $value;
}
public function sortEdit(Form $form)
{
throw new RuntimeException('This repository does not support "sortEdit" method.');
}
private $treePidColumn = 'pid';
private $treeTitleColumn = 'title';
private $treeSortColumn = 'sort';
public function getTreePidColumn()
{
return $this->treePidColumn;
}
public function setTreePidColumn($value)
{
$this->treePidColumn = $value;
}
public function getTreeTitleColumn()
{
return $this->treeTitleColumn;
}
public function setTreeTitleColumn($value)
{
$this->treeTitleColumn = $value;
}
public function getTreeSortColumn()
{
return $this->treeSortColumn;
}
public function setTreeSortColumn($value)
{
$this->treeSortColumn = $value;
}
public function getTreeItems($context)
{
throw new \RuntimeException('This repository does not support "getTreeItems" method.');
}
/**
* @return array
*/
public function getTreeAncestorItems()
{
throw new \RuntimeException('This repository does not support "getTreeAncestorItems" method.');
}
/**
* 构建 Repository
* @param $params string
*
* @return $this
*/
public static function make(...$params)
{
return new static(...$params);
}
/**
* 根据输入各种输入构建数据仓库
* @param mixed $repository
* @param array $args
* @return Repository
*/
public static function instance($repository, array $args = [])
{
/**
* 部分场景只需要构建表单
*/
if (null === $repository) {
return null;
}
if (is_string($repository)) {
$repository = new $repository($args);
}
if ($repository instanceof \Illuminate\Database\Eloquent\Model
|| $repository instanceof \Illuminate\Database\Eloquent\Builder) {
$repository = EloquentRepository::make($repository);
}
if (!$repository instanceof \ModStart\Repository\Repository) {
$class = is_object($repository) ? get_class($repository) : $repository;
throw new \InvalidArgumentException("The class [{$class}] must be a type of [" . Repository::class . '].');
}
return $repository;
}
}