-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathEntity.php
More file actions
350 lines (306 loc) · 7.73 KB
/
Copy pathEntity.php
File metadata and controls
350 lines (306 loc) · 7.73 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think;
use ArrayAccess;
use InvalidArgumentException;
use JsonSerializable;
use ReflectionClass;
use think\contract\Arrayable;
use think\contract\Jsonable;
use think\model\contract\Modelable;
use WeakMap;
/**
* Class Entity.
* @mixin Model
*/
abstract class Entity implements JsonSerializable, ArrayAccess, Arrayable, Jsonable, Modelable
{
private static ?WeakMap $weakMap = null;
/**
* 架构函数.
*
* @param Model $model 模型连接对象
*/
public function __construct(?Model $model = null)
{
$this->initWeakMap();
// 获取实体模型参数
$baseOptions = $this->getBaseOptions();
$options = $this->getOptions();
foreach (['viewMapping', 'autoMapping'] as $item) {
$options[$item] = array_merge($baseOptions[$item] ?? [], $options[$item] ?? []);
}
$options = array_merge($baseOptions, $options);
if (is_null($model)) {
$class = !empty($options['modelClass']) ? $options['modelClass'] : str_replace('\\entity\\', '\\model\\', static::class);
$model = new $class();
}
$model->entity($this);
self::$weakMap[$this] = [
'model' => $model,
];
// 初始化模型
$this->setOptions($options);
$this->init($options);
}
protected function initWeakMap()
{
if (!self::$weakMap) {
self::$weakMap = new WeakMap;
}
}
/**
* 定义实体模型的基础配置参数.
*
* @return array
*/
protected function getBaseOptions(): array
{
return [];
}
/**
* 定义实体模型相关配置参数.
*
* @return array
*/
protected function getOptions(): array
{
return [];
}
/**
* 批量设置模型参数
* @param array $options 值
* @return void
*/
public function setOptions(array $options): void
{
foreach ($options as $name => $value) {
$this->setOption($name, $value);
}
}
/**
* 设置模型参数
*
* @param string $name 参数名
* @param mixed $value 值
*
* @return $this
*/
public function setOption(string $name, $value)
{
self::$weakMap[$this][$name] = $value;
return $this;
}
/**
* 获取模型参数
*
* @param string $name 参数名
* @param mixed $default 默认值
*
* @return mixed
*/
public function getOption(string $name, $default = null)
{
return self::$weakMap[$this][$name] ?? $default;
}
/**
* 创建新的实例.
*
* @param Model $model 模型连接对象
* @param array $options 查询参数
*/
public function newInstance(?Model $model, array $options = [])
{
$entity = new static();
return $entity->setModel($model, $options);
}
/**
* 初始化模型.
*
* @param array $options 模型参数
* @return void
*/
protected function init(array $options = []): void {}
/**
* 获取模型对象实例.
* @return Model
*/
public function model()
{
return self::$weakMap[$this]['model'];
}
/**
* 设置模型.
*
* @param Model $model 模型对象
* @return $this
*/
public function setModel(Model $model)
{
self::$weakMap[$this]['model'] = $model;
return $this;
}
/**
* 获取克隆的模型实例.
*
* @return static
*/
public function clone()
{
$model = new static();
self::$weakMap[$model] = self::$weakMap[$this];
return $model;
}
/**
* 克隆模型实例
*
* @return void
*/
public function __clone()
{
throw new InvalidArgumentException('use $modelObj->clone() replace clone $modelObj');
}
/**
* 序列化模型对象
*
* @return array
*/
public function __serialize(): array
{
return array_diff_key(self::$weakMap[$this]);
}
/**
* 反序列化模型对象
*
* @param array $data
* @return void
*/
public function __unserialize(array $data)
{
$this->initWeakMap();
self::$weakMap[$this] = $data;
}
/**
* 获取属性 支持获取器
*
* @param string $name 名称
*
* @return mixed
*/
public function __get(string $name)
{
return $this->model()->get($name);
}
/**
* 设置数据 支持类型自动转换
*
* @param string $name 名称
* @param mixed $value 值
*
* @return void
*/
public function __set(string $name, $value): void
{
$this->model()->set($name, $value);
}
/**
* 检测数据对象的值
*
* @param string $name 名称
*
* @return bool
*/
public function __isset(string $name): bool
{
return $this->model()->__isset($name);
}
/**
* 销毁数据对象的值
*
* @param string $name 名称
*
* @return void
*/
public function __unset(string $name): void
{
$this->model()->__unset($name);
}
public function __toString()
{
return $this->model()->toJson();
}
public function __debugInfo()
{
return $this->model()->getData();
}
// JsonSerializable
public function jsonSerialize(): array
{
return $this->model()->toArray();
}
/**
* 模型数据转数组.
*
* @return array
*/
public function toArray(): array
{
return $this->model()->toArray();
}
/**
* 模型数据转Json.
*
* @param int $options json参数
* @return string
*/
public function toJson(int $options = JSON_UNESCAPED_UNICODE): string
{
return $this->model()->toJson($options);
}
// ArrayAccess
public function offsetSet(mixed $name, mixed $value): void
{
$this->__set($name, $value);
}
public function offsetGet(mixed $name): mixed
{
return $this->__get($name);
}
public function offsetExists(mixed $name): bool
{
return $this->__isset($name);
}
public function offsetUnset(mixed $name): void
{
$this->__unset($name);
}
public static function __callStatic($method, $args)
{
$entity = new static();
$modelClass = get_class($entity->model());
$staticMethods = (new \ReflectionClass($modelClass))->getMethods(\ReflectionMethod::IS_STATIC);
$staticPublicMethods = array_filter($staticMethods, fn($m) => $m->isPublic());
if (in_array($method, array_column($staticPublicMethods, 'name'))) {
// 调用model的静态方法
$db = $modelClass;
} else {
// 调用Query类查询方法
$db = $entity->model()->db();
}
return call_user_func_array([$db, $method], $args);
}
public function __call($method, $args)
{
// 调用Model类方法
return call_user_func_array([$this->model(), $method], $args);
}
}