TextControl /
txtextcontrol-reportingcloud-php
| 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://git.io/Jejj2 for the canonical source repository |
||
| 11 | * @license https://git.io/Jejjr |
||
| 12 | * @copyright © 2022 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 | use ValueToStringTrait; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Check value is a valid DateTime string |
||
| 35 | * |
||
| 36 | * @param string $value |
||
| 37 | * @param string $message |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | * @throws InvalidArgumentException |
||
| 41 | */ |
||
| 42 | 16 | public static function assertDateTime(string $value, string $message = ''): void |
|
| 43 | { |
||
| 44 | 16 | $timeZone = ReportingCloud::DEFAULT_TIME_ZONE; |
|
| 45 | 16 | $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT; |
|
| 46 | |||
| 47 | 16 | if (self::getDateTimeLength() !== strlen($value)) { |
|
| 48 | 2 | $format = 0 === strlen($message) ? '%1$s has an invalid number of characters in it' : $message; |
|
| 49 | 2 | $message = sprintf($format, self::valueToString($value)); |
|
| 50 | 2 | throw new InvalidArgumentException($message); |
|
| 51 | } |
||
| 52 | |||
| 53 | 14 | $dateTimeZone = new DateTimeZone($timeZone); |
|
| 54 | |||
| 55 | try { |
||
| 56 | 14 | $dateTime = DateTime::createFromFormat($dateFormat, $value, $dateTimeZone); |
|
| 57 | 14 | if ($dateTime instanceof DateTime) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 58 | 10 | if (0 !== $dateTime->getOffset()) { |
|
| 59 | 2 | $format = 0 === strlen($message) ? '%1$s has an invalid offset' : $message; |
|
| 60 | 2 | $message = sprintf($format, self::valueToString($value)); |
|
| 61 | 10 | throw new InvalidArgumentException($message); |
|
| 62 | } |
||
| 63 | } else { |
||
| 64 | 4 | $format = 0 === strlen($message) ? '%1$s is syntactically invalid' : $message; |
|
| 65 | 4 | $message = sprintf($format, self::valueToString($value)); |
|
| 66 | 12 | throw new InvalidArgumentException($message); |
|
| 67 | } |
||
| 68 | 6 | } catch (Exception $e) { |
|
| 69 | // @phpstan-ignore-next-line |
||
| 70 | 6 | $format = 0 === strlen($message) ? 'Internal error validating %1$s - %2$s' : $message; |
|
| 71 | 6 | $message = sprintf($format, self::valueToString($value), self::valueToString($e->getMessage())); |
|
| 72 | 6 | throw new InvalidArgumentException($message); |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the length of the required dateTime string |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | 16 | private static function getDateTimeLength(): int |
|
| 82 | { |
||
| 83 | 16 | $ret = 0; |
|
| 84 | |||
| 85 | 16 | $timeZone = ReportingCloud::DEFAULT_TIME_ZONE; |
|
| 86 | 16 | $dateFormat = ReportingCloud::DEFAULT_DATE_FORMAT; |
|
| 87 | |||
| 88 | 16 | $dateTimeZone = new DateTimeZone($timeZone); |
|
| 89 | |||
| 90 | try { |
||
| 91 | 16 | $dateTime = new DateTime('now', $dateTimeZone); |
|
| 92 | 16 | $ret = strlen($dateTime->format($dateFormat)); |
|
| 93 | 16 | unset($dateTime); |
|
| 94 | 16 | unset($dateTimeZone); |
|
| 95 | } catch (Exception $e) { |
||
| 96 | // continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | 16 | return $ret; |
|
| 100 | } |
||
| 101 | } |
||
| 102 |