Completed
Push — master ( 4030be...df6e71 )
by Tobias
10:01
created

DBALTypesResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B tryGetTypeForValue() 0 24 4
A addType() 0 4 1
1
<?php
2
3
namespace Happyr\DoctrineSpecification;
4
5
use Doctrine\DBAL\Types\Type;
6
7
final class DBALTypesResolver
8
{
9
    /**
10
     * The map of supported doctrine mapping types.
11
     *
12
     * @var array
13
     */
14
    private static $typesMap = array();
15
16
    /**
17
     * Try get type for value.
18
     *
19
     * If type is not found return NULL.
20
     *
21
     * @param mixed $value
22
     *
23
     * @return Type|null
24
     */
25
    public static function tryGetTypeForValue($value)
26
    {
27
        if (!is_object($value)) {
28
            return null;
29
        }
30
31
        // maybe it's a ValueObject
32
33
        // try get type name from types map
34
        $className = get_class($value);
35
        if (isset(self::$typesMap[$className])) {
36
            return Type::getType(self::$typesMap[$className]);
37
        }
38
39
        // use class name as type name
40
        $classNameParts = explode('\\', $className);
41
        $typeName = array_pop($classNameParts);
42
43
        if (array_key_exists($typeName, Type::getTypesMap())) {
44
            return Type::getType($typeName);
45
        }
46
47
        return null;
48
    }
49
50
    /**
51
     * Adds a custom type to the type map for resolve Value Object.
52
     *
53
     * @param string $name      the name of the type
54
     * @param string $className the class name of the Value Object
55
     */
56
    public static function addType($name, $className)
57
    {
58
        self::$typesMap[$className] = $name;
59
    }
60
}
61