1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cycle ORM Schema Builder. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Schema\Definition\Map; |
13
|
|
|
|
14
|
|
|
use Cycle\Schema\Definition\Field; |
15
|
|
|
use Cycle\Schema\Exception\FieldException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Manage the set of fields associated with the entity. |
19
|
|
|
*/ |
20
|
|
|
final class FieldMap implements \IteratorAggregate, \Countable |
21
|
|
|
{ |
22
|
|
|
/** @var Field[] */ |
23
|
|
|
private $fields = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Cloning. |
27
|
|
|
*/ |
28
|
|
|
public function __clone() |
29
|
|
|
{ |
30
|
|
|
foreach ($this->fields as $name => $field) { |
31
|
|
|
$this->fields[$name] = clone $field; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public function count(): int |
39
|
|
|
{ |
40
|
|
|
return count($this->fields); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get field column names |
45
|
|
|
*/ |
46
|
|
|
public function getColumnNames(): array |
47
|
|
|
{ |
48
|
|
|
return array_values(array_map(static function (Field $field) { |
49
|
|
|
return $field->getColumn(); |
50
|
|
|
}, $this->fields)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get property names |
55
|
|
|
*/ |
56
|
|
|
public function getNames(): array |
57
|
|
|
{ |
58
|
|
|
return array_keys($this->fields); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function has(string $name): bool |
66
|
|
|
{ |
67
|
|
|
return isset($this->fields[$name]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check if field with given column name exist |
72
|
|
|
*/ |
73
|
|
|
public function hasColumn(string $name): bool |
74
|
|
|
{ |
75
|
|
|
foreach ($this->fields as $field) { |
76
|
|
|
if ($field->getColumn() === $name) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get field by property name |
86
|
|
|
*/ |
87
|
|
|
public function get(string $name): Field |
88
|
|
|
{ |
89
|
|
|
if (!$this->has($name)) { |
90
|
|
|
throw new FieldException("Undefined field `{$name}`."); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->fields[$name]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get property name by column name |
98
|
|
|
*/ |
99
|
|
|
public function getKeyByColumnName(string $name): string |
100
|
|
|
{ |
101
|
|
|
foreach ($this->fields as $key => $field) { |
102
|
|
|
if ($field->getColumn() === $name) { |
103
|
|
|
return $key; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new FieldException("Undefined field with column name `{$name}`."); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get field by column name |
112
|
|
|
*/ |
113
|
|
|
public function getByColumnName(string $name): Field |
114
|
|
|
{ |
115
|
|
|
foreach ($this->fields as $field) { |
116
|
|
|
if ($field->getColumn() === $name) { |
117
|
|
|
return $field; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
throw new FieldException("Undefined field with column name `{$name}`."); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $name |
126
|
|
|
* @param Field $field |
127
|
|
|
* @return FieldMap |
128
|
|
|
*/ |
129
|
|
|
public function set(string $name, Field $field): self |
130
|
|
|
{ |
131
|
|
|
if ($this->has($name)) { |
132
|
|
|
throw new FieldException("Field `{$name}` already exists."); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->fields[$name] = $field; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @return FieldMap |
143
|
|
|
*/ |
144
|
|
|
public function remove(string $name): self |
145
|
|
|
{ |
146
|
|
|
unset($this->fields[$name]); |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return Field[]|\Traversable |
152
|
|
|
*/ |
153
|
|
|
public function getIterator() |
154
|
|
|
{ |
155
|
|
|
return new \ArrayIterator($this->fields); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|