Passed
Push — dev ( b20a94...1d0b99 )
by 世昌
02:26
created

TableStruct::fields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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