Passed
Push — master ( f0f4fd...011363 )
by Anton
01:46
created

FieldMap::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Schema\Definition\Map;
10
11
use Cycle\Schema\Definition\Field;
12
use Cycle\Schema\Exception\FieldException;
13
14
/**
15
 * Manage the set of fields associated with the entity.
16
 */
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()
82
    {
83
        return new \ArrayIterator($this->fields);
84
    }
85
}