DBALTypesResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A tryGetTypeForValue() 0 24 5
A addType() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification;
15
16
use Doctrine\DBAL\Types\Type;
17
18
final class DBALTypesResolver
19
{
20
    /**
21
     * The map of supported doctrine mapping types.
22
     *
23
     * @var array
24
     */
25
    private static $typesMap = [];
26
27
    /**
28
     * Try get type for value.
29
     *
30
     * If type is not found return NULL.
31
     *
32
     * @param mixed $value
33
     *
34
     * @return Type|null
35
     */
36
    public static function tryGetTypeForValue($value)
37
    {
38
        if (!is_object($value)) {
39
            return null;
40
        }
41
42
        // maybe it's a ValueObject
43
44
        // try get type name from types map
45
        $className = get_class($value);
46
        if (isset(self::$typesMap[$className])) {
47
            return Type::getType(self::$typesMap[$className]);
48
        }
49
50
        // use class name as type name
51
        $classNameParts = explode('\\', str_replace('_', '\\', $className));
52
        $typeName = array_pop($classNameParts);
53
54
        if (null !== $typeName && array_key_exists($typeName, Type::getTypesMap())) {
55
            return Type::getType($typeName);
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Adds a custom type to the type map for resolve Value Object.
63
     *
64
     * @param string $name      the name of the type
65
     * @param string $className the class name of the Value Object
66
     */
67
    public static function addType($name, $className)
68
    {
69
        self::$typesMap[$className] = $name;
70
    }
71
}
72