1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP SDK |
6
|
|
|
* |
7
|
|
|
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Assert; |
16
|
|
|
|
17
|
|
|
use DateTime; |
18
|
|
|
use DateTimeZone; |
19
|
|
|
use Exception; |
20
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
21
|
|
|
use TxTextControl\ReportingCloud\ReportingCloud; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Trait AssertDateTimeTrait |
25
|
|
|
* |
26
|
|
|
* @package TxTextControl\ReportingCloud |
27
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
28
|
|
|
*/ |
29
|
|
|
trait AssertDateTimeTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param mixed $value |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
abstract protected static function valueToString($value): string; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check value is a valid DateTime string |
40
|
|
|
* |
41
|
|
|
* @param string $value |
42
|
|
|
* @param string $message |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
*/ |
47
|
24 |
|
public static function assertDateTime(string $value, string $message = ''): void |
48
|
|
|
{ |
49
|
24 |
|
$timeZone = ReportingCloud::DEFAULT_TIME_ZONE; |
50
|
24 |
|
$dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT; |
51
|
|
|
|
52
|
24 |
|
if (self::getDateTimeLength() !== strlen($value)) { |
53
|
3 |
|
$format = $message ?: '%1$s has an invalid number of characters in it'; |
54
|
3 |
|
$message = sprintf($format, self::valueToString($value)); |
55
|
3 |
|
throw new InvalidArgumentException($message); |
56
|
|
|
} |
57
|
|
|
|
58
|
21 |
|
$dateTimeZone = new DateTimeZone($timeZone); |
59
|
|
|
|
60
|
|
|
try { |
61
|
21 |
|
$dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone); |
62
|
21 |
|
if ($dateTime instanceof DateTime) { |
63
|
15 |
|
if (0 !== $dateTime->getOffset()) { |
64
|
3 |
|
$format = $message ?: '%1$s has an invalid offset'; |
65
|
3 |
|
$message = sprintf($format, self::valueToString($value)); |
66
|
15 |
|
throw new InvalidArgumentException($message); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
6 |
|
$format = $message ?: '%1$s is syntactically invalid'; |
70
|
6 |
|
$message = sprintf($format, self::valueToString($value)); |
71
|
18 |
|
throw new InvalidArgumentException($message); |
72
|
|
|
} |
73
|
9 |
|
} catch (Exception $e) { |
74
|
9 |
|
$format = $message ?: 'Internal error validating %1$s - %2$s'; |
75
|
9 |
|
$message = sprintf($format, self::valueToString($value), self::valueToString($e->getMessage())); |
76
|
9 |
|
throw new InvalidArgumentException($message); |
77
|
|
|
} |
78
|
12 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the length of the required dateTime string |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
24 |
|
private static function getDateTimeLength() |
86
|
|
|
{ |
87
|
24 |
|
$ret = 0; |
88
|
|
|
|
89
|
24 |
|
$timeZone = ReportingCloud::DEFAULT_TIME_ZONE; |
90
|
24 |
|
$dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT; |
91
|
|
|
|
92
|
24 |
|
$dateTimeZone = new DateTimeZone($timeZone); |
93
|
|
|
|
94
|
|
|
try { |
95
|
24 |
|
$dateTime = new DateTime('now', $dateTimeZone); |
96
|
24 |
|
$ret = strlen($dateTime->format($dateFormat)); |
97
|
|
|
} catch (Exception $e) { |
98
|
|
|
// continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
24 |
|
unset($dateTimeZone, $dateTime); |
102
|
|
|
|
103
|
24 |
|
return $ret; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|