Passed
Push — master ( c79051...5600d0 )
by 世昌
02:50 queued 11s
created

TableStruct   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 23

18 Methods

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