Total Complexity | 10 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
18 | final class FieldMap implements \IteratorAggregate, \Countable |
||
19 | { |
||
20 | /** @var Field[] */ |
||
21 | private $fields = []; |
||
22 | |||
23 | /** |
||
24 | * @return int |
||
25 | */ |
||
26 | public function count(): int |
||
27 | { |
||
28 | return count($this->fields); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param string $name |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function has(string $name): bool |
||
36 | { |
||
37 | return isset($this->fields[$name]); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $name |
||
42 | * @return Field |
||
43 | */ |
||
44 | public function get(string $name): Field |
||
45 | { |
||
46 | if (!$this->has($name)) { |
||
47 | throw new FieldException("Undefined field `{$name}`"); |
||
48 | } |
||
49 | |||
50 | return $this->fields[$name]; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $name |
||
55 | * @param Field $field |
||
56 | * @return FieldMap |
||
57 | */ |
||
58 | public function set(string $name, Field $field): self |
||
59 | { |
||
60 | if ($this->has($name)) { |
||
61 | throw new FieldException("Field `{$name}` already exists"); |
||
62 | } |
||
63 | |||
64 | $this->fields[$name] = $field; |
||
65 | |||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | * @return FieldMap |
||
72 | */ |
||
73 | public function remove(string $name): self |
||
74 | { |
||
75 | unset($this->fields[$name]); |
||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return Field[]|\Traversable |
||
81 | */ |
||
82 | public function getIterator() |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Cloning. |
||
89 | */ |
||
90 | public function __clone() |
||
96 | } |