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