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