Completed
Branch master (09022f)
by Gareth
05:56 queued 03:06
created

Caster::castExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

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