1
|
|
|
<?php |
2
|
|
|
namespace suda\orm\struct; |
3
|
|
|
|
4
|
|
|
use function array_key_exists; |
5
|
|
|
use function array_search; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use Traversable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class TableStruct |
12
|
|
|
* @package suda\orm\struct |
13
|
|
|
*/ |
14
|
|
|
class TableStruct implements IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* 数据表名 |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* 字段集合 |
25
|
|
|
* |
26
|
|
|
* @var Field[] |
27
|
|
|
*/ |
28
|
|
|
protected $fields; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 键值对映射 |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $alias; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 创建字段集合 |
39
|
|
|
* |
40
|
|
|
* @param string $table |
41
|
|
|
*/ |
42
|
|
|
public function __construct(string $table) |
43
|
|
|
{ |
44
|
|
|
$this->name = $table; |
45
|
|
|
$this->fields = []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 新建表列 |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param string $type |
53
|
|
|
* @param int|array $length |
54
|
|
|
* @return Field |
55
|
|
|
*/ |
56
|
|
|
public function field(string $name, string $type, $length = null) |
57
|
|
|
{ |
58
|
|
|
return $this->fields[$name] ?? $this->fields[$name] = ($length?new Field($this->name, $name, $type, $length):new Field($this->name, $name, $type)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @param string $type |
64
|
|
|
* @param mixed $length |
65
|
|
|
* @return Field |
66
|
|
|
*/ |
67
|
|
|
public function newField(string $name, string $type, $length = null) |
68
|
|
|
{ |
69
|
|
|
return $this->fields[$name] ?? $this->fields[$name] = ($length?new Field($this->name, $name, $type, $length):new Field($this->name, $name, $type)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @return Field|null |
75
|
|
|
*/ |
76
|
|
|
public function getField(string $name) |
77
|
|
|
{ |
78
|
|
|
return $this->fields[$name] ?? null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 添加表结构字段 |
83
|
|
|
* |
84
|
|
|
* @param array|Field $fields |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function fields($fields) |
88
|
|
|
{ |
89
|
|
|
if (!is_array($fields) && $fields instanceof Field) { |
90
|
|
|
$fields = func_get_args(); |
91
|
|
|
} |
92
|
|
|
foreach ($fields as $field) { |
93
|
|
|
$this->addField($field); |
94
|
|
|
} |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasField(string $name) |
103
|
|
|
{ |
104
|
|
|
return array_key_exists($name, $this->fields); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Field $field |
109
|
|
|
*/ |
110
|
|
|
public function addField(Field $field) |
111
|
|
|
{ |
112
|
|
|
if ($field->getTableName() != $this->name) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
$name = $field->getName(); |
116
|
|
|
$this->fields[$name] = $field; |
117
|
|
|
$this->alias[$name] = $field->getAlias(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function outputName(string $name):string |
125
|
|
|
{ |
126
|
|
|
if (array_key_exists($name, $this->alias)) { |
127
|
|
|
return $this->alias[$name]; |
128
|
|
|
} |
129
|
|
|
return $name; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function inputName(string $name):string |
137
|
|
|
{ |
138
|
|
|
if ($key = array_search($name, $this->alias)) { |
139
|
|
|
return $key; |
140
|
|
|
} |
141
|
|
|
return $name; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getFieldsName() |
148
|
|
|
{ |
149
|
|
|
return array_keys($this->fields); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the value of name |
154
|
|
|
*/ |
155
|
|
|
public function getName() |
156
|
|
|
{ |
157
|
|
|
return $this->name; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $name |
162
|
|
|
*/ |
163
|
|
|
public function setName(string $name): void |
164
|
|
|
{ |
165
|
|
|
$this->name = $name; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the value of fields |
170
|
|
|
*/ |
171
|
|
|
public function all() |
172
|
|
|
{ |
173
|
|
|
return $this->fields; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return ArrayIterator|Traversable |
178
|
|
|
*/ |
179
|
|
|
public function getIterator() |
180
|
|
|
{ |
181
|
|
|
return new ArrayIterator($this->fields); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|