Completed
Push — master ( 126036...3fa63d )
by Maik
07:56
created

OrmDataTypeConverter   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 79.69%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 30
c 6
b 1
f 1
lcom 1
cbo 4
dl 0
loc 153
ccs 51
cts 64
cp 0.7969
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B convertDate() 0 26 6
C convertType() 0 44 15
A getColumnType() 0 4 1
C convertToDatabaseType() 0 29 8
1
<?php
2
namespace Nkey\Caribu\Orm;
3
4
/**
5
 * Datatype conversion provider for Caribu Orm
6
 *
7
 * This class is part of Caribu package
8
 *
9
 * @author Maik Greubel <[email protected]>
10
 */
11
trait OrmDataTypeConverter
12
{
13
    /**
14
     * Include some basic utility functions
15
     */
16
    use OrmUtil;
17
18
    /**
19
     * Convert a date string into DateTime object
20
     *
21
     * Accepted values are (in this order)
22
     *
23
     *  - Unix timestamp
24
     *  - Single ISO8601 date
25
     *  - Simple ISO8601 date and time without timezone
26
     *  - DateTime::W3C
27
     *  - DateTime::ISO8601
28
     *
29
     * @param string $value
30
     */
31 14
    private static function convertDate($value)
32
    {
33
        try {
34 14
            $date = new \DateTime(sprintf("@%s", $value));
35 14
        } catch (\Exception $exception) {
36
            try {
37 13
                $date = \DateTime::createFromFormat("Y-m-d", $value);
38
39 13
                if (!$date) {
40 13
                    $date = \DateTime::createFromFormat("Y-m-d H:i:s", $value);
41 13
                }
42
43 13
                if (!$date) {
44 1
                    $date = \DateTime::createFromFormat(\DateTime::W3C, $value);
45 1
                }
46
47 13
                if (!$date) {
48 1
                    $date = \DateTime::createFromFormat(\DateTime::ISO8601, $value);
49 1
                }
50 13
            } catch (\Exception $exception) {
51
                throw OrmException::fromPrevious($exception);
52
            }
53
        }
54
55 14
        return $date;
56
    }
57
58
    /**
59
     * Convert arbitrary data into given type
60
     *
61
     * @param string $type The type to convert data into
62
     * @param mixed $value The value to convert
63
     * @throws OrmException
64
     *
65
     * @return mixed The converted data
66
     */
67 26
    private static function convertType($type, $value)
68
    {
69 26
        if (!$type) {
70 5
            return $value;
71
        }
72
73 22
        if ($value instanceof $type) {
74 13
            return $value;
75
        }
76
77
        switch ($type) {
78 22
            case 'bool':
79 22
            case 'boolean':
80
                return self::boolval($value);
81
82 22
            case 'int':
83 22
            case 'integer':
84 22
            case 'number':
85 1
                return intval($value);
86
87 22
            case 'float':
88 22
            case 'double':
89 22
            case 'real':
90 22
            case 'decimal':
91
                return doubleval($value);
92
93 22
            case 'string':
94 22
                return strval($value);
95
96 14
            default:
97 14
                if (!self::isInternalClass($type)) {
98
                    return $value;
99
                }
100 14
        }
101
102 14
        $rf = new \ReflectionClass($type);
103 14
        if ($rf->name == 'DateTime') {
104 14
            return self::convertDate($value);
105
        }
106
107
        throw new OrmException("Unknown type {type}", array(
108
            'type' => $type
109
        ));
110
    }
111
112
    /**
113
     * Retrieve the type of column in database
114
     *
115
     * @param Orm $orm Orm instance
116
     * @param string $table The table where the column is part of
117
     * @param string $columnName The name of column to retrieve datatype of
118
     *
119
     * @return int The datatype of column in database table
120
     */
121 11
    private static function getColumnType(Orm $orm, $table, $columnName)
122
    {
123 11
        return $orm->getDbType()->getColumnType($table, $columnName, $orm);
124
    }
125
126
    /**
127
     * Convert type into database type representation
128
     *
129
     * @param integer $type The type to convert from
130
     * @param mixed $value The value to convert from
131
     *
132
     * @return mixed The converted value data
133
     */
134 11
    private static function convertToDatabaseType($type, $value)
135
    {
136
        switch ($type) {
137 11
            case OrmDataType::STRING:
138 11
                return strval($value);
139
140 7
            case OrmDataType::DECIMAL:
141
                return doubleval($value);
142
143 7
            case OrmDataType::INTEGER:
144 7
                if ($value instanceof \DateTime) {
145
                    return $value->getTimestamp();
146
                }
147 7
                return intval($value);
148
149 2
            case OrmDataType::BLOB:
150
                return strval($value);
151
152 2
            case OrmDataType::DATETIME:
153 2
                if ($value instanceof \DateTime) {
154 2
                    return $value->format('Y-m-d H:i:s');
155
                }
156
                return strval($value);
157
158
            default:
159
                return strval($value);
160
161
        }
162
    }
163
}
164