Test Failed
Push — master ( aff3ac...48396d )
by 世昌
02:34
created

TableStruct::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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