Passed
Push — sudav3 ( 36433a...9b090b )
by 世昌
02:24
created

Fields::outputName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\struct;
3
4
use ArrayIterator;
5
use IteratorAggregate;
6
use suda\orm\struct\Field;
7
8
class Fields implements IteratorAggregate
9
{
10
    /**
11
     * 数据表名
12
     *
13
     * @var string
14
     */
15
    protected $name;
16
17
    /**
18
     * 字段集合
19
     *
20
     * @var Field[]
21
     */
22
    protected $fields;
23
24
    /**
25
     * 键值对映射
26
     *
27
     * @var array
28
     */
29
    protected $alias;
30
31
    /**
32
     * 创建字段集合
33
     *
34
     * @param string $table
35
     */
36
    public function __construct(string $table)
37
    {
38
        $this->name = $table;
39
        $this->fields = [];
40
    }
41
42
    /**
43
     * 新建表列
44
     *
45
     * @param string $name
46
     * @param string $type
47
     * @param int|array $length
48
     * @return Field
49
     */
50
    public function field(string $name, string $type, $length = null)
51
    {
52
        return $this->fields[$name] ?? $this->fields[$name] = ($length?new Field($this->name, $name, $type, $length):new Field($this->name, $name, $type));
53
    }
54
55
    public function newField(string $name, string $type, $length = null)
56
    {
57
        return $this->fields[$name] ?? $this->fields[$name] = ($length?new Field($this->name, $name, $type, $length):new Field($this->name, $name, $type));
58
    }
59
60
    public function getField(string $name)
61
    {
62
        return $this->fields[$name] ?? null;
63
    }
64
65
    public function hasField(string $name)
66
    {
67
        return array_key_exists($name, $this->fields);
68
    }
69
70
    public function addField(Field $field)
71
    {
72
        if ($field->getTableName() != $this->name) {
73
            return;
74
        }
75
        $name = $field->getName();
76
        $this->fields[$name] = $field;
77
        $this->alias[$name] = $field->getAlias();
78
    }
79
80
    public function outputName(string $name):string {
81
        if (\array_key_exists($name, $this->alias)) {
82
            return $this->alias[$name];
83
        }
84
        return $name;
85
    }
86
87
    public function inputName(string $name):string {
88
        if ($key = \array_search($name, $this->alias)) {
89
            return $key;
90
        }
91
        return $name;
92
    }
93
94
    public function getFieldsName()
95
    {
96
        return array_keys($this->fields);
97
    }
98
99
    /**
100
     * Get the value of name
101
     */
102
    public function getName()
103
    {
104
        return $this->name;
105
    }
106
107
    /**
108
     * Get the value of fields
109
     */
110
    public function all()
111
    {
112
        return $this->fields;
113
    }
114
115
    public function getIterator()
116
    {
117
        return new ArrayIterator($this->fields);
118
    }
119
}
120