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