EnumRegistry::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yokai\EnumBundle;
6
7
use Yokai\EnumBundle\Exception\DuplicatedEnumException;
8
use Yokai\EnumBundle\Exception\InvalidEnumException;
9
10
/**
11
 * @author Yann Eugoné <[email protected]>
12
 */
13
class EnumRegistry
14
{
15
    /**
16
     * @var EnumInterface[]
17
     */
18
    private $enums;
19
20
    /**
21
     * @param EnumInterface $enum
22
     *
23
     * @throws DuplicatedEnumException
24
     */
25
    public function add(EnumInterface $enum): void
26
    {
27
        if ($this->has($enum->getName())) {
28
            throw DuplicatedEnumException::alreadyRegistered($enum->getName());
29
        }
30
31
        $this->enums[$enum->getName()] = $enum;
32
    }
33
34
    /**
35
     * @param string $name
36
     *
37
     * @return EnumInterface
38
     * @throws InvalidEnumException
39
     */
40
    public function get(string $name): EnumInterface
41
    {
42
        if (!$this->has($name)) {
43
            throw InvalidEnumException::nonexistent($name);
44
        }
45
46
        return $this->enums[$name];
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return bool
53
     */
54
    public function has(string $name): bool
55
    {
56
        return isset($this->enums[$name]);
57
    }
58
59
    /**
60
     * @return EnumInterface[]
61
     */
62
    public function all(): array
63
    {
64
        return $this->enums;
65
    }
66
}
67