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