Issues (22)

src/Assert/AssertDateTimeTrait.php (1 issue)

Severity
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://tinyurl.com/vmbbh6kd for the canonical source repository
11
 * @license   https://tinyurl.com/3pc9am89
12
 * @copyright © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\ReportingCloud\Assert;
16
17
use DateTime;
18
use DateTimeZone;
19
use Exception;
20
use TextControl\ReportingCloud\Exception\InvalidArgumentException;
21
use TextControl\ReportingCloud\ReportingCloud;
22
23
/**
24
 * Trait AssertDateTimeTrait
25
 */
26
trait AssertDateTimeTrait
27
{
28
    use ValueToStringTrait;
29
30
    /**
31
     * Check value is a valid DateTime string
32
     */
33 16
    public static function assertDateTime(string $value, string $message = ''): void
34
    {
35 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
36 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
37
38 16
        if (self::getDateTimeLength() !== strlen($value)) {
39 2
            $format  = '' === $message ? '%1$s has an invalid number of characters in it' : $message;
40 2
            $message = sprintf($format, self::valueToString($value));
41 2
            throw new InvalidArgumentException($message);
42
        }
43
44 14
        $dateTimeZone = new DateTimeZone($timeZone);
45
46
        try {
47 14
            $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone);
48 14
            if ($dateTime instanceof DateTime) {
0 ignored issues
show
$dateTime is always a sub-type of DateTime.
Loading history...
49 10
                if (0 !== $dateTime->getOffset()) {
50 2
                    $format  = '' === $message ? '%1$s has an invalid offset' : $message;
51 2
                    $message = sprintf($format, self::valueToString($value));
52 10
                    throw new InvalidArgumentException($message);
53
                }
54
            } else {
55 4
                $format  = '' === $message ? '%1$s is syntactically invalid' : $message;
56 4
                $message = sprintf($format, self::valueToString($value));
57 12
                throw new InvalidArgumentException($message);
58
            }
59 6
        } catch (Exception $exception) {
60
            // @phpstan-ignore-next-line
61 6
            $format  = '' === $message ? 'Internal error validating %1$s - %2$s' : $message;
62 6
            $message = sprintf($format, self::valueToString($value), self::valueToString($exception->getMessage()));
63 6
            throw new InvalidArgumentException($message);
64
        }
65
    }
66
67
    /**
68
     * Get the length of the required dateTime string
69
     */
70 16
    private static function getDateTimeLength(): int
71
    {
72 16
        $ret = 0;
73
74 16
        $timeZone   = ReportingCloud::DEFAULT_TIME_ZONE;
75 16
        $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT;
76
77 16
        $dateTimeZone = new DateTimeZone($timeZone);
78
79
        try {
80 16
            $dateTime = new DateTime('now', $dateTimeZone);
81 16
            $ret      = strlen($dateTime->format($dateFormat));
82 16
            unset($dateTime);
83 16
            unset($dateTimeZone);
84
        } catch (Exception) {
85
            // continue;
86
        }
87
88 16
        return $ret;
89
    }
90
}
91