|
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
|
|
|
* @param string $name |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function has(string $name): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return isset($this->fields[$name]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @return Field |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get(string $name): Field |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->has($name)) { |
|
69
|
|
|
throw new FieldException("Undefined field `{$name}`."); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->fields[$name]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* @param Field $field |
|
78
|
|
|
* @return FieldMap |
|
79
|
|
|
*/ |
|
80
|
|
|
public function set(string $name, Field $field): self |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->has($name)) { |
|
83
|
|
|
throw new FieldException("Field `{$name}` already exists."); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->fields[$name] = $field; |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @return FieldMap |
|
94
|
|
|
*/ |
|
95
|
|
|
public function remove(string $name): self |
|
96
|
|
|
{ |
|
97
|
|
|
unset($this->fields[$name]); |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return Field[]|\Traversable |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getIterator() |
|
105
|
|
|
{ |
|
106
|
|
|
return new \ArrayIterator($this->fields); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|