Passed
Push — develop ( 09565d...6daf93 )
by Sergei
03:16
created

TypeRegistry::getMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Types;
6
7
use Doctrine\DBAL\DBALException;
8
use function array_search;
9
use function in_array;
10
11
/**
12
 * The type registry is responsible for holding a map of all known DBAL types.
13
 * The types are stored using the flyweight pattern so that one type only exists as exactly one instance.
14
 *
15
 * @internal TypeRegistry exists for forward compatibility, its API should not be considered stable.
16
 */
17
final class TypeRegistry
18
{
19
    /** @var array<string, Type> Map of type names and their corresponding flyweight objects. */
20
    private $instances = [];
21
22
    /**
23
     * Finds a type by the given name.
24
     *
25
     * @throws DBALException
26
     */
27 32368
    public function get(string $name) : Type
28
    {
29 32368
        if (! isset($this->instances[$name])) {
30 1101
            throw DBALException::unknownColumnType($name);
31
        }
32
33 32368
        return $this->instances[$name];
34
    }
35
36
    /**
37
     * Finds a name for the given type.
38
     *
39
     * @throws DBALException
40
     */
41 1051
    public function lookupName(Type $type) : string
42
    {
43 1051
        $name = $this->findTypeName($type);
44
45 1051
        if ($name === null) {
46 1051
            throw DBALException::typeNotRegistered($type);
47
        }
48
49 1051
        return $name;
50
    }
51
52
    /**
53
     * Checks if there is a type of the given name.
54
     */
55 29444
    public function has(string $name) : bool
56
    {
57 29444
        return isset($this->instances[$name]);
58
    }
59
60
    /**
61
     * Registers a custom type to the type map.
62
     *
63
     * @throws DBALException
64
     */
65 29177
    public function register(string $name, Type $type) : void
66
    {
67 29177
        if (isset($this->instances[$name])) {
68 977
            throw DBALException::typeExists($name);
69
        }
70
71 29177
        if ($this->findTypeName($type) !== null) {
72
            throw DBALException::typeAlreadyRegistered($type);
73
        }
74
75 29177
        $this->instances[$name] = $type;
76 29177
    }
77
78
    /**
79
     * Overrides an already defined type to use a different implementation.
80
     *
81
     * @throws DBALException
82
     */
83 929
    public function override(string $name, Type $type) : void
84
    {
85 929
        if (! isset($this->instances[$name])) {
86 876
            throw DBALException::typeNotFound($name);
87
        }
88
89 928
        if (! in_array($this->findTypeName($type), [$name, null], true)) {
90 901
            throw DBALException::typeAlreadyRegistered($type);
91
        }
92
93 927
        $this->instances[$name] = $type;
94 927
    }
95
96
    /**
97
     * Gets the map of all registered types and their corresponding type instances.
98
     *
99
     * @internal
100
     *
101
     * @return array<string, Type>
102
     */
103 31700
    public function getMap() : array
104
    {
105 31700
        return $this->instances;
106
    }
107
108 29177
    private function findTypeName(Type $type) : ?string
109
    {
110 29177
        $name = array_search($type, $this->instances, true);
111
112 29177
        if ($name === false) {
113 29177
            return null;
114
        }
115
116 1053
        return $name;
117
    }
118
}
119