|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ReportingCloud PHP Wrapper |
|
6
|
|
|
* |
|
7
|
|
|
* PHP wrapper 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 Throwable; |
|
20
|
|
|
use TxTextControl\ReportingCloud\ReportingCloud; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Trait AssertDateTimeTrait |
|
24
|
|
|
* |
|
25
|
|
|
* @package TxTextControl\ReportingCloud |
|
26
|
|
|
*/ |
|
27
|
|
|
trait AssertDateTimeTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Validate DateTime string |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $value |
|
33
|
|
|
* @param string $message |
|
34
|
|
|
* |
|
35
|
|
|
* @return null |
|
36
|
|
|
*/ |
|
37
|
52 |
|
public static function assertDateTime(string $value, string $message = '') |
|
38
|
|
|
{ |
|
39
|
52 |
|
$timeZone = ReportingCloud::DEFAULT_TIME_ZONE; |
|
40
|
52 |
|
$dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT; |
|
41
|
|
|
|
|
42
|
52 |
|
$dateTimeZone = new DateTimeZone($timeZone); |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
52 |
|
$dateTime = new DateTime('now', $dateTimeZone); |
|
46
|
52 |
|
if (strlen($dateTime->format($dateFormat)) !== strlen($value)) { |
|
47
|
1 |
|
$format = '%s has an invalid number of characters in it'; |
|
48
|
1 |
|
$message = sprintf($message ?: $format, static::valueToString($value)); |
|
49
|
52 |
|
static::reportInvalidArgument($message); |
|
50
|
|
|
} |
|
51
|
1 |
|
} catch (Throwable $e) { |
|
52
|
1 |
|
$format = 'Internal error validating %s'; |
|
53
|
1 |
|
$message = sprintf($message ?: $format, static::valueToString($value)); |
|
54
|
1 |
|
static::reportInvalidArgument($message); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
51 |
|
$dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone); |
|
59
|
51 |
|
if (!$dateTime) { |
|
60
|
2 |
|
$format = '%s is syntactically invalid'; |
|
61
|
2 |
|
$message = sprintf($message ?: $format, static::valueToString($value)); |
|
62
|
2 |
|
static::reportInvalidArgument($message); |
|
63
|
|
|
} |
|
64
|
49 |
|
if (0 !== $dateTime->getOffset()) { |
|
65
|
1 |
|
$format = '%s has an invalid offset'; |
|
66
|
1 |
|
$message = sprintf($message ?: $format, static::valueToString($value)); |
|
67
|
49 |
|
static::reportInvalidArgument($message); |
|
68
|
|
|
} |
|
69
|
3 |
|
} catch (Throwable $e) { |
|
70
|
3 |
|
$format = 'Internal error validating %s'; |
|
71
|
3 |
|
$message = sprintf($message ?: $format, static::valueToString($value)); |
|
72
|
3 |
|
static::reportInvalidArgument($message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
48 |
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|