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

RelationMap::__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\Relation;
13
use Cycle\Schema\Exception\RelationException;
14
15
final class RelationMap implements \IteratorAggregate
16
{
17
    /** @var Relation[] */
18
    private $relations = [];
19
20
    /**
21
     * @param string $name
22
     * @return bool
23
     */
24
    public function has(string $name): bool
25
    {
26
        return isset($this->relations[$name]);
27
    }
28
29
    /**
30
     * @param string $name
31
     * @return Relation
32
     */
33
    public function get(string $name): Relation
34
    {
35
        if (!$this->has($name)) {
36
            throw new RelationException("Undefined relation `{$name}`");
37
        }
38
39
        return $this->relations[$name];
40
    }
41
42
    /**
43
     * @param string   $name
44
     * @param Relation $relation
45
     * @return RelationMap
46
     */
47
    public function set(string $name, Relation $relation): self
48
    {
49
        if ($this->has($name)) {
50
            throw new RelationException("Relation `{$name}` already exists");
51
        }
52
53
        $this->relations[$name] = $relation;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $name
60
     * @return RelationMap
61
     */
62
    public function remove(string $name): self
63
    {
64
        unset($this->relations[$name]);
65
        return $this;
66
    }
67
68
    /**
69
     * @return Relation[]|\Traversable
70
     */
71
    public function getIterator()
72
    {
73
        return new \ArrayIterator($this->relations);
74
    }
75
76
    /**
77
     * Cloning.
78
     */
79
    public function __clone()
80
    {
81
        foreach ($this->relations as $name => $relation) {
82
            $this->relations[$name] = clone $relation;
83
        }
84
    }
85
}