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

Fields   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 151
rs 10
c 0
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newField() 0 3 2
A addField() 0 8 2
A getField() 0 3 1
A field() 0 3 2
A hasField() 0 3 1
A all() 0 3 1
A setName() 0 3 1
A inputName() 0 6 2
A outputName() 0 6 2
A getName() 0 3 1
A getFieldsName() 0 3 1
A getIterator() 0 3 1
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 Fields
12
 * @package suda\orm\struct
13
 */
14
class Fields 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
     * @param string $name
83
     * @return bool
84
     */
85
    public function hasField(string $name)
86
    {
87
        return array_key_exists($name, $this->fields);
88
    }
89
90
    /**
91
     * @param Field $field
92
     */
93
    public function addField(Field $field)
94
    {
95
        if ($field->getTableName() != $this->name) {
96
            return;
97
        }
98
        $name = $field->getName();
99
        $this->fields[$name] = $field;
100
        $this->alias[$name] = $field->getAlias();
101
    }
102
103
    /**
104
     * @param string $name
105
     * @return string
106
     */
107
    public function outputName(string $name):string
108
    {
109
        if (array_key_exists($name, $this->alias)) {
110
            return $this->alias[$name];
111
        }
112
        return $name;
113
    }
114
115
    /**
116
     * @param string $name
117
     * @return string
118
     */
119
    public function inputName(string $name):string
120
    {
121
        if ($key = array_search($name, $this->alias)) {
122
            return $key;
123
        }
124
        return $name;
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function getFieldsName()
131
    {
132
        return array_keys($this->fields);
133
    }
134
135
    /**
136
     * Get the value of name
137
     */
138
    public function getName()
139
    {
140
        return $this->name;
141
    }
142
143
    /**
144
     * @param string $name
145
     */
146
    public function setName(string $name): void
147
    {
148
        $this->name = $name;
149
    }
150
151
    /**
152
     * Get the value of fields
153
     */
154
    public function all()
155
    {
156
        return $this->fields;
157
    }
158
159
    /**
160
     * @return ArrayIterator|Traversable
161
     */
162
    public function getIterator()
163
    {
164
        return new ArrayIterator($this->fields);
165
    }
166
}
167