Failed Conditions
Pull Request — master (#3354)
by Michael
12:22
created

TypeRegistry::getType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
nc 3
nop 1
crap 3
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 get_class;
10
11
/**
12
 * @internal
13
 */
14
final class TypeRegistry
15
{
16
    /**
17
     * Map of already instantiated type objects. One instance per type (flyweight).
18
     *
19
     * @var Type[]
20
     */
21
    private $typeInstances = [];
22
23
    /**
24
     * Map of defined types and their classes
25
     *
26
     * @var string[]
27
     */
28
    private $typesMap = [];
29
30
    /**
31
     * Factory method to create type instances.
32
     * Type instances are implemented as flyweights.
33
     *
34
     * @throws DBALException
35
     */
36 42265
    public function getType(string $name) : Type
37
    {
38 42265
        if (isset($this->typeInstances[$name])) {
39 42121
            return $this->typeInstances[$name];
40
        }
41
42 211
        if (isset($this->typesMap[$name])) {
43 211
            return $this->typeInstances[$name] = new $this->typesMap[$name]();
44
        }
45
46 24
        throw DBALException::unknownColumnType($name);
47
    }
48
49
    /**
50
     * Find a name for the given type.
51
     *
52
     * @throws DBALException
53
     */
54 24
    public function getName(Type $type) : string
55
    {
56 24
        $name = array_search($type, $this->typeInstances, true);
57
58 24
        if ($name === false) {
59 24
            throw DBALException::typeNotFound(get_class($type));
60
        }
61
62 24
        return $name;
63
    }
64
65
    /**
66
     * Checks if there is a type of the given name.
67
     */
68 3193
    public function hasType(string $name) : bool
69
    {
70 3193
        return isset($this->typesMap[$name]);
71
    }
72
73
    /**
74
     * Adds a custom type to the type map.
75
     *
76
     * @param string $name      The name of the type. This should correspond to what getName() returns.
77
     * @param string $className The class name of the custom type.
78
     *
79
     * @throws DBALException
80
     */
81 259
    public function addType(string $name, string $className) : void
82
    {
83 259
        if (isset($this->typesMap[$name])) {
84
            throw DBALException::typeExists($name);
85
        }
86
87 259
        $this->typesMap[$name] = $className;
88 259
    }
89
90
    /**
91
     * Overrides an already defined type to use a different implementation.
92
     *
93
     * @throws DBALException
94
     */
95 24
    public function overrideType(string $name, string $className) : void
96
    {
97 24
        if (! isset($this->typesMap[$name])) {
98
            throw DBALException::typeNotFound($name);
99
        }
100
101 24
        if (isset($this->typeInstances[$name])) {
102
            unset($this->typeInstances[$name]);
103
        }
104
105 24
        $this->typesMap[$name] = $className;
106 24
    }
107
108
    /**
109
     * Gets the types array map which holds all registered types and the corresponding
110
     * type class
111
     *
112
     * @return string[]
113
     */
114 23598
    public function getTypesMap() : array
115
    {
116 23598
        return $this->typesMap;
117
    }
118
}
119