|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\orm\struct; |
|
3
|
|
|
|
|
4
|
|
|
use ArrayIterator; |
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use suda\orm\struct\MagicArrayAccessTrait; |
|
7
|
|
|
|
|
8
|
|
|
trait ArrayDataTrait |
|
9
|
|
|
{ |
|
10
|
|
|
use MagicArrayAccessTrait; |
|
11
|
|
|
use SimpleJsonDataTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* 表数据 |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $data = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* 设置值 |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $name |
|
24
|
|
|
* @param mixed $value |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __set(string $name, $value) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->assertFieldName($name); |
|
29
|
|
|
$this->data[$name] = $value; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* 获取参数值 |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __get(string $name) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->assertFieldName($name); |
|
41
|
|
|
return $this->data[$name] ?? null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* 判断是否设置 |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* @return boolean |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __isset(string $name) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertFieldName($name); |
|
53
|
|
|
return array_key_exists($name, $this->data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* 取消设置值 |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __unset(string $name) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->assertFieldName($name); |
|
64
|
|
|
unset($this->data[$name]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* 断言字段 |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function assertFieldName(string $name) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->checkFieldExist($name) === false) { |
|
76
|
|
|
throw new InvalidArgumentException(sprintf('[%s] has no attribute %s', static::class, $name), 0); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* 检查字段是否存在 |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
|
|
public function checkFieldExist(string $name) |
|
87
|
|
|
{ |
|
88
|
|
|
return strlen($name) > 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 获取迭代器 |
|
93
|
|
|
* |
|
94
|
|
|
* @return ArrayIterator |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getIterator() |
|
97
|
|
|
{ |
|
98
|
|
|
return new ArrayIterator($this->data); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* 转换成原始数组 |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function toArray():array |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 计数 |
|
113
|
|
|
* |
|
114
|
|
|
* @return int |
|
115
|
|
|
*/ |
|
116
|
|
|
public function count() |
|
117
|
|
|
{ |
|
118
|
|
|
return count($this->data); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getExportJsonData() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->data; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|