Completed
Push — master ( 5049eb...794713 )
by Gareth
03:08
created

Caster::cast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3
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
    private static function getValueType($value)
31
    {
32 10
        $fromType = gettype($value);
33 10
        if ($fromType == "object") {
34 2
            $fromType = get_class($value);
35 2
        }
36
37 10
        return $fromType;
38
    }
39
40 10
    public static function shouldCast($value, $type)
41
    {
42 10
        $fromType = self::getValueType($value);
43
        if ($fromType == $type
44 10
            || self::isTypeAnAlias($fromType, $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
    private static function isTypeAnAlias($fromType, $type)
60
    {
61
        $aliases = [
62 9
            'DateTime' => 'DateTime',
63 9
            'dateTime' => 'DateTime',
64 9
            'date' => 'DateTime',
65
            'time' => 'DateTime'
66 9
        ];
67
68 9
        return (isset($aliases[$type]) && $aliases[$type] == $fromType);
69
    }
70
71 18
    private static function getCasts()
72
    {
73
        return [
74
            'DateTime' => [
75
                'string' => function ($value) {
76 2
                    return new \DateTime($value);
77
                }
78 18
            ],
79
            'dateTime' => [
80
                'string' => function ($value) {
81 1
                    return new \DateTime($value);
82
                }
83 18
            ],
84
            'date' => [
85
                'string' => function ($value) {
86
                    return new \DateTime($value);
87
                }
88 18
            ],
89
            'time' => [
90
                'string' => function ($value) {
91 3
                    return new \DateTime($value);
92
                }
93 18
            ],
94
            'ExchangeFormat' => [
95
                'DateTime' => function ($value) {
96 1
                    return $value->format('c');
97 18
                },
98
                'dateTime' => function ($value) {
99
                    return $value->format('c');
100 18
                },
101
                'date' => function ($value) {
102
                    return $value->format('Y-m-d');
103 18
                },
104
                'time' => function ($value) {
105
                    return $value->format('H:i:s');
106
                }
107 18
            ]
108 18
        ];
109
    }
110
}
111