Completed
Push — master ( 9e9528...25f4f6 )
by Gareth
04:01
created

Caster::getValueType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: gareth
5
 * Date: 20-7-15
6
 * Time: 16:27
7
 */
8
9
namespace garethp\ews;
10
11
class Caster
12
{
13
14
    public static function cast($value, $type)
15
    {
16
        $casts = self::getCasts();
17
        $fromType = self::getValueType($value);
18
19
        if (!self::shouldCast($value, $type)) {
20
            return $value;
21
        }
22
23
        if (!self::castExists($fromType, $type)) {
24
            return null;
25
        }
26
27
        return $casts[$type][$fromType]($value);
28
    }
29
30
    public static function getValueType($value)
31
    {
32
        $fromType = gettype($value);
33
        if ($fromType == "object") {
34
            $fromType = get_class($value);
35
        }
36
37
        return $fromType;
38
    }
39
40
    public static function shouldCast($value, $type)
41
    {
42
        $fromType = self::getValueType($value);
43
        if ($fromType == $type
44
            || (isset(self::getCastMap()[$type]) && $fromType == self::getCastMap()[$type])
45
            || ($type == "ExchangeFormat" && gettype($value) !== "object")) {
46
            return false;
47
        }
48
49
        return true;
50
    }
51
52
    public static function castExists($from, $to)
53
    {
54
        $casts = self::getCasts();
55
56
        return !(empty($casts[$to][$from]));
57
    }
58
59
    public static function getCastMap()
60
    {
61
        return [
62
            'DateTime' => 'DateTime',
63
            'dateTime' => 'DateTime',
64
            'date' => 'DateTime',
65
            'time' => 'DateTime'
66
        ];
67
    }
68
69
    private static function getCasts()
70
    {
71
        return [
72
            'DateTime' => [
73
                'string' => function ($value) {
74
                    return new \DateTime($value);
75
                }
76
            ],
77
            'dateTime' => [
78
                'string' => function ($value) {
79
                    return new \DateTime($value);
80
                }
81
            ],
82
            'date' => [
83
                'string' => function ($value) {
84
                    return new \DateTime($value);
85
                }
86
            ],
87
            'time' => [
88
                'string' => function ($value) {
89
                    return new \DateTime($value);
90
                }
91
            ],
92
            'ExchangeFormat' => [
93
                'DateTime' => function ($value) {
94
                    return $value->format('c');
95
                },
96
                'dateTime' => function ($value) {
97
                    return $value->format('c');
98
                },
99
                'date' => function ($value) {
100
                    return $value->format('Y-m-d');
101
                },
102
                'time' => function ($value) {
103
                    return $value->format('H:i:s');
104
                }
105
            ]
106
        ];
107
    }
108
}
109