Passed
Push — master ( 1fa407...e70c1d )
by Anton
04:02
created

FieldMap::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Cycle ORM Schema Builder.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
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, \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()
83
    {
84
        return new \ArrayIterator($this->fields);
85
    }
86
87
    /**
88
     * Cloning.
89
     */
90
    public function __clone()
91
    {
92
        foreach ($this->fields as $name => $field) {
93
            $this->fields[$name] = clone $field;
94
        }
95
    }
96
}