1
|
|
|
<?php |
2
|
|
|
namespace suda\orm; |
3
|
|
|
|
4
|
|
|
use suda\orm\struct\Field; |
5
|
|
|
use suda\orm\struct\TableStruct; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use suda\orm\struct\ArrayDataTrait; |
8
|
|
|
use suda\orm\struct\ArrayDataInterface; |
9
|
|
|
|
10
|
|
|
class TableData implements ArrayDataInterface |
11
|
|
|
{ |
12
|
|
|
use ArrayDataTrait; |
13
|
|
|
/** |
14
|
|
|
* 数据表名 |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 数据表列 |
22
|
|
|
* |
23
|
|
|
* @var TableStruct |
24
|
|
|
*/ |
25
|
|
|
protected $struct; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 创建表结构 |
29
|
|
|
* |
30
|
|
|
* @param string $name |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $name) |
33
|
|
|
{ |
34
|
|
|
$this->name = $name; |
35
|
|
|
$this->struct = new TableStruct($name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function createAll(array $data) |
39
|
|
|
{ |
40
|
|
|
foreach ($data as $index => $row) { |
41
|
|
|
$data[$index] = $this->createOne($row); |
42
|
|
|
} |
43
|
|
|
return $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function createOne(array $data) |
47
|
|
|
{ |
48
|
|
|
$struct = new self($this->name); |
49
|
|
|
$struct->data = $data; |
50
|
|
|
$struct->struct = $this->struct; |
51
|
|
|
return $struct; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function offsetSet($offset, $value) |
55
|
|
|
{ |
56
|
|
|
$this->__set($offset, $value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function offsetExists($offset) |
60
|
|
|
{ |
61
|
|
|
return $this->__isset($offset); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function offsetUnset($offset) |
65
|
|
|
{ |
66
|
|
|
$this->__unset($offset); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function offsetGet($offset) |
70
|
|
|
{ |
71
|
|
|
return $this->__get($offset); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get 数据表名 |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getName():string |
81
|
|
|
{ |
82
|
|
|
return $this->name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $name |
87
|
|
|
*/ |
88
|
|
|
public function setName(string $name): void |
89
|
|
|
{ |
90
|
|
|
$this->name = $name; |
91
|
|
|
$this->struct->setName($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get 数据表列 |
96
|
|
|
* |
97
|
|
|
* @return TableStruct |
98
|
|
|
*/ |
99
|
|
|
public function getStruct():TableStruct |
100
|
|
|
{ |
101
|
|
|
return $this->struct; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 设置值 |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* @param mixed $value |
109
|
|
|
*/ |
110
|
|
|
public function __set(string $name, $value) |
111
|
|
|
{ |
112
|
|
|
$this->assertFieldName($name); |
113
|
|
|
$this->data[$name] = $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 获取参数值 |
118
|
|
|
* |
119
|
|
|
* @param string $name |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function __get(string $name) |
123
|
|
|
{ |
124
|
|
|
$this->assertFieldName($name); |
125
|
|
|
return $this->data[$name] ?? null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* 判断是否设置 |
130
|
|
|
* |
131
|
|
|
* @param string $name |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
public function __isset(string $name) |
135
|
|
|
{ |
136
|
|
|
$this->assertFieldName($name); |
137
|
|
|
return array_key_exists($name, $this->data); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 取消设置值 |
142
|
|
|
* |
143
|
|
|
* @param string $name |
144
|
|
|
*/ |
145
|
|
|
public function __unset(string $name) |
146
|
|
|
{ |
147
|
|
|
$this->assertFieldName($name); |
148
|
|
|
unset($this->data[$name]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function assertFieldName(string $name) |
152
|
|
|
{ |
153
|
|
|
if ($this->struct->hasField($name) === false) { |
154
|
|
|
throw new InvalidArgumentException(sprintf('TableStruct[%s] has no attribute %s', $this->name, $name), 0); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|