Passed
Branch development-2.0 (f1893c)
by Jonathan
06:14
created

AssertDateTimeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 29
dl 0
loc 49
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B assertDateTime() 0 39 11
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