Passed
Push — master ( c984d5...8532b4 )
by Anton
02:38
created

FieldMap::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Definition\Map;
11
12
use Cycle\Schema\Definition\Field;
13
use Cycle\Schema\Exception\FieldException;
14
15
/**
16
 * Manage the set of fields associated with the entity.
17
 */
18
final class FieldMap implements \IteratorAggregate
19
{
20
    /** @var Field[] */
21
    private $fields = [];
22
23
    /**
24
     * @param string $name
25
     * @return bool
26
     */
27
    public function has(string $name): bool
28
    {
29
        return isset($this->fields[$name]);
30
    }
31
32
    /**
33
     * @param string $name
34
     * @return Field
35
     */
36
    public function get(string $name): Field
37
    {
38
        if (!$this->has($name)) {
39
            throw new FieldException("Undefined field `{$name}`");
40
        }
41
42
        return $this->fields[$name];
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param Field  $field
48
     * @return FieldMap
49
     */
50
    public function set(string $name, Field $field): self
51
    {
52
        if ($this->has($name)) {
53
            throw new FieldException("Field `{$name}` already exists");
54
        }
55
56
        $this->fields[$name] = $field;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @return FieldMap
64
     */
65
    public function remove(string $name): self
66
    {
67
        unset($this->fields[$name]);
68
        return $this;
69
    }
70
71
    /**
72
     * @return Field[]|\Traversable
73
     */
74
    public function getIterator()
75
    {
76
        return new \ArrayIterator($this->fields);
77
    }
78
}