Passed
Push — master ( 5431c9...e4edfb )
by Anton
01:36
created

FieldMap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A set() 0 9 2
A getAll() 0 3 1
A has() 0 3 1
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
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
     * Get named list of all fields.
63
     *
64
     * @return array
65
     */
66
    public function getAll(): array
67
    {
68
        return $this->fields;
69
    }
70
}