Completed
Push — master ( 023690...819b2d )
by Sébastien
06:02 queued 10s
created

Type::createColumnDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6667
cc 2
eloc 5
nc 2
nop 4
crap 2.032
1
<?php
2
namespace Soluble\Db\Metadata\Column;
3
4
use Soluble\Db\Metadata\Column\Exception;
5
6
class Type
7
{
8
    const TYPE_INTEGER  = 'integer';
9
    const TYPE_DECIMAL  = 'decimal';
10
    const TYPE_STRING   = 'string';
11
    const TYPE_BOOLEAN  = 'boolean';
12
    const TYPE_DATETIME = 'datetime';
13
    const TYPE_BLOB     = 'blob';
14
    const TYPE_DATE     = 'date';
15
    const TYPE_TIME     = 'time';
16
    const TYPE_FLOAT    = 'float';
17
    const TYPE_BIT      = 'bit';
18
    const TYPE_SPATIAL_GEOMETRY = 'geometry';
19
20
    /**
21
     * @var array
22
     */
23
    protected static $typesMap = array(
24
25
        self::TYPE_INTEGER  => 'Definition\IntegerColumn',
26
        self::TYPE_DECIMAL  => 'Definition\DecimalColumn',
27
        self::TYPE_STRING   => 'Definition\StringColumn',
28
        self::TYPE_BOOLEAN  => 'Definition\BooleanColumn',
29
        self::TYPE_DATETIME     => 'Definition\DatetimeColumn',
30
        self::TYPE_BLOB         => 'Definition\BlobColumn',
31
        self::TYPE_DATE         => 'Definition\DateColumn',
32
        self::TYPE_TIME         => 'Definition\TimeColumn',
33
        self::TYPE_FLOAT    => 'Definition\FloatColumn',
34
        self::TYPE_BIT      => 'Definition\BitColumn',
35
        self::TYPE_SPATIAL_GEOMETRY         => 'Definition\GeometryColumn',
36
    );
37
38
39
40
    /**
41
     *
42
     * @param string $datatype
43
     * @param string $name
44
     * @param string $tableName
45
     * @param string $schemaName
46
     * @throws Exception\UnsupportedDatatypeException
47
     * @return \Soluble\Db\Metadata\Column\Definition\AbstractColumnDefinition
48
     */
49 17
    public static function createColumnDefinition($datatype, $name, $tableName = null, $schemaName = null)
50
    {
51 17
        if (!array_key_exists($datatype, self::$typesMap)) {
52
            throw new Exception\UnsupportedDatatypeException(__METHOD__ . " Type '$datatype' is not supported.");
53
        }
54 17
        $class = __NAMESPACE__ . '\\' . self::$typesMap[$datatype];
55
56 17
        return new $class($name, $tableName, $schemaName);
57
    }
58
    
59
    /**
60
     * Return all supported types
61
     *
62
     * @return array
63
     */
64 1
    public static function getSupportedTypes()
65
    {
66 1
        return array_keys(self::$typesMap);
67
    }
68
}
69