|
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
|
|
|
} |
|
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
|
9 |
|
|| 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
|
|
|
'dateTime' => 'DateTime', |
|
64
|
|
|
'date' => 'DateTime', |
|
65
|
|
|
'time' => 'DateTime' |
|
66
|
|
|
]; |
|
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
|
18 |
|
} |
|
78
|
|
|
], |
|
79
|
|
|
'dateTime' => [ |
|
80
|
|
|
'string' => function ($value) { |
|
81
|
1 |
|
return new \DateTime($value); |
|
82
|
18 |
|
} |
|
83
|
|
|
], |
|
84
|
|
|
'date' => [ |
|
85
|
|
|
'string' => function ($value) { |
|
86
|
|
|
return new \DateTime($value); |
|
87
|
18 |
|
} |
|
88
|
|
|
], |
|
89
|
|
|
'time' => [ |
|
90
|
|
|
'string' => function ($value) { |
|
91
|
3 |
|
return new \DateTime($value); |
|
92
|
18 |
|
} |
|
93
|
|
|
], |
|
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
|
18 |
|
} |
|
107
|
|
|
] |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|