Test Failed
Pull Request — master (#2834)
by Grégoire
07:06
created

TypeRegistry::getType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Types;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use Doctrine\DBAL\DBALException;
24
25
/**
26
 * The registry for so-called Doctrine mapping types.
27
 *
28
 * A Type object is obtained by calling the static {@link getType()} method.
29
 *
30
 * @author Roman Borschel <[email protected]>
31
 * @author Benjamin Eberlei <[email protected]>
32
 * @since  2.0
33
 */
34
final class TypeRegistry
35
{
36
    /**
37
     * Map of already instantiated type objects. One instance per type (flyweight).
38
     *
39
     * @var array
40
     */
41
    private static $_typeObjects = array();
42
43
    /**
44
     * The map of supported doctrine mapping types for the current platform.
45
     *
46
     * @var array
47
     */
48
    private static $_typesMap = array(
49
        ArrayType::class,
50
        SimpleArrayType::class,
51
        JsonArrayType::class,
52
        JsonType::class,
53
        ObjectType::class,
54
        BooleanType::class,
55
        IntegerType::class,
56
        SmallIntType::class,
57
        BigIntType::class,
58
        StringType::class,
59
        TextType::class,
60
        DateTimeType::class,
61
        DateTimeImmutableType::class,
62
        DateTimeTzType::class,
63
        DateTimeTzImmutableType::class,
64
        DateType::class,
65
        DateImmutableType::class,
66
        TimeType::class,
67
        TimeImmutableType::class,
68
        DecimalType::class,
69
        FloatType::class,
70
        BinaryType::class,
71
        BlobType::class,
72
        GuidType::class,
73
        DateIntervalType::class,
74
    );
75
76
    /**
77
     * Factory method to create type instances.
78
     * Type instances are implemented as flyweights.
79
     *
80
     * @param string $class The FQCN of the type
81
     *
82
     * @return \Doctrine\DBAL\Types\Type
83
     *
84
     * @throws \Doctrine\DBAL\DBALException
85
     */
86 1497
    public static function getType(string $class): Doctrine\DBAL\Types\Type
87
    {
88 1497
        if (!self::hasType($class)) {
89 1496
            throw DBALException::unknownColumnType($class);
90
        }
91 1
        if (!isset(self::$_typeObjects[$class])) {
92 1
            self::$_typeObjects[$class] = new $class();
93
        }
94
95 1
        return self::$_typeObjects[$class];
96
    }
97
98
    /**
99
     * Adds a custom type to the type map.
100
     *
101
     * @param string $class The class name of the custom type.
102
     *
103
     * @return void
104
     *
105
     * @throws \Doctrine\DBAL\DBALException
106
     */
107 1
    public static function addType(string $class)
108
    {
109 1
        if (self::hasType($class)) {
110
            throw DBALException::typeExists($name);
111
        }
112
113 1
        self::$_typesMap[] = $class;
114 1
    }
115
116
    /**
117
     * Checks if exists support for a type.
118
     *
119
     * @param string $class The classname of the type.
120
     *
121
     * @return boolean TRUE if type is supported; FALSE otherwise.
122
     */
123 1497
    public static function hasType(string $class): bool
124
    {
125 1497
        return in_array($class, self::$_typesMap);
126
    }
127
128
    /**
129
     * Gets the types array map which holds all registered types and the corresponding
130
     * type class
131
     *
132
     * @return array
133
     */
134
    public static function getTypesMap()
135
    {
136
        return self::$_typesMap;
137
    }
138
}
139