1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SetBased\ClubCollect\Helper; |
5
|
|
|
|
6
|
|
|
use SetBased\Helper\InvalidCastException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @inheritDoc |
10
|
|
|
*/ |
11
|
|
|
class Cast extends \SetBased\Helper\Cast |
12
|
|
|
{ |
13
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
14
|
|
|
/** |
15
|
|
|
* Converts a value to a string. If the value can not be safely casted to a string throws an exception. |
16
|
|
|
* |
17
|
|
|
* @param mixed $value The value. |
18
|
|
|
* @param string|null $default The default value. If the value is null and the default is not null the default value |
19
|
|
|
* will be returned. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
* |
23
|
|
|
* @throws InvalidCastException |
24
|
|
|
*/ |
25
|
|
|
public static function toManString($value, ?string $default = null): string |
26
|
|
|
{ |
27
|
|
|
$ret = parent::toManString($value, $default); |
28
|
|
|
|
29
|
|
|
if ($ret==='') |
30
|
|
|
{ |
31
|
|
|
throw new InvalidCastException('Value can not be converted to string'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $ret; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
38
|
|
|
/** |
39
|
|
|
* Converts a value to a DateTime. If the value can not be safely casted to a DateTime throws an exception. |
40
|
|
|
* |
41
|
|
|
* @param mixed $value The value. |
42
|
|
|
* @param string|null $default The default value. If the value is null the default value will be returned. |
43
|
|
|
* |
44
|
|
|
* @return \DateTime|null |
45
|
|
|
* |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public static function toOptDateTime($value, ?string $default = null): ?\DateTime |
49
|
|
|
{ |
50
|
|
|
$epoch = static::toOptString($value, $default); |
51
|
|
|
|
52
|
|
|
if ($epoch===null) return null; |
53
|
|
|
|
54
|
|
|
return new \DateTime($epoch); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
58
|
|
|
/** |
59
|
|
|
* Converts a value to a DateTime. If the value can not be safely casted to a DateTime throws an exception. |
60
|
|
|
* |
61
|
|
|
* @param mixed $value The value. |
62
|
|
|
* @param string|null $default The default value. If the value is null the default value will be returned. |
63
|
|
|
* |
64
|
|
|
* @return \DateTime |
65
|
|
|
* |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public static function toManDateTime($value, ?string $default = null): ?\DateTime |
69
|
|
|
{ |
70
|
|
|
$epoch = static::toManString($value, $default); |
71
|
|
|
|
72
|
|
|
return new \DateTime($epoch); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
76
|
|
|
/** |
77
|
|
|
* Converts a value to a string. If the value can not be safely casted to a string throws an exception. |
78
|
|
|
* |
79
|
|
|
* @param mixed $value The value. |
80
|
|
|
* @param string|null $default The default value. If the value is null the default value will be returned. |
81
|
|
|
* |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
|
|
public static function toOptString($value, ?string $default = null): ?string |
85
|
|
|
{ |
86
|
|
|
if ($value==='') return null; |
87
|
|
|
|
88
|
|
|
return parent::toOptString($value, $default); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
95
|
|
|
|