Skip to content

Commit d150534

Browse files
committed
added verbose error in case of failed date construction
1 parent ffad7fe commit d150534

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/Formatter/Specialised/HttpOGMStringTranslator.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313

1414
namespace Laudis\Neo4j\Formatter\Specialised;
1515

16+
use function count;
17+
use function date_get_last_errors;
1618
use DateInterval;
1719
use DateTimeImmutable;
1820
use Exception;
1921
use function explode;
2022
use Iterator;
23+
use function json_encode;
24+
use const JSON_THROW_ON_ERROR;
25+
use JsonException;
2126
use Laudis\Neo4j\Types\Date;
2227
use Laudis\Neo4j\Types\DateTime;
2328
use Laudis\Neo4j\Types\Duration;
2429
use Laudis\Neo4j\Types\LocalDateTime;
2530
use Laudis\Neo4j\Types\LocalTime;
2631
use Laudis\Neo4j\Types\Time;
32+
use RuntimeException;
2733
use function str_pad;
2834
use function substr;
2935

@@ -101,11 +107,17 @@ private function translateDuration(string $value): Duration
101107
return new Duration($months, $days, $seconds, $nanoseconds);
102108
}
103109

110+
/**
111+
* @throws JsonException
112+
*/
104113
private function translateDate(string $value): Date
105114
{
106115
$epoch = new DateTimeImmutable('@0');
107-
/** @psalm-suppress PossiblyFalseReference */
108-
$diff = DateTimeImmutable::createFromFormat('Y-m-d', $value)->diff($epoch);
116+
$date = DateTimeImmutable::createFromFormat('Y-m-d', $value);
117+
if ($date === false) {
118+
throw new RuntimeException(json_encode(date_get_last_errors(), JSON_THROW_ON_ERROR));
119+
}
120+
$diff = $date->diff($epoch);
109121

110122
return new Date((int) $diff->format('%a'));
111123
}
@@ -119,6 +131,9 @@ private function translateTime(string $value): Time
119131
return new Time(((int) $values[0]) * 60 * 60 + ((int) $values[1]) * 60);
120132
}
121133

134+
/**
135+
* @throws Exception
136+
*/
122137
private function translateDateTime(string $value): DateTime
123138
{
124139
[$date, $time] = explode('T', $value);
@@ -131,34 +146,41 @@ private function translateDateTime(string $value): DateTime
131146
[$time, $milliseconds] = explode('.', $time);
132147

133148
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time);
149+
if ($date === false) {
150+
throw new RuntimeException(json_encode(date_get_last_errors(), JSON_THROW_ON_ERROR));
151+
}
134152

135153
if ($tz !== null) {
136-
/** @psalm-suppress PossiblyFalseReference */
137154
return new DateTime($date->getTimestamp(), (int) $milliseconds * 1000000, $tz);
138155
}
139156

140-
/** @psalm-suppress PossiblyFalseReference */
141157
return new DateTime($date->getTimestamp(), (int) $milliseconds * 1000000, 0);
142158
}
143159

160+
/**
161+
* @throws JsonException
162+
*/
144163
private function translateLocalDateTime(string $value): LocalDateTime
145164
{
146165
[$date, $time] = explode('T', $value);
147166
[$time, $milliseconds] = explode('.', $time);
148167

149168
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date.' '.$time);
169+
if ($date === false) {
170+
throw new RuntimeException(json_encode(date_get_last_errors(), JSON_THROW_ON_ERROR));
171+
}
150172

151-
/** @psalm-suppress PossiblyFalseReference */
152173
return new LocalDateTime($date->getTimestamp(), (int) $milliseconds * 1000000);
153174
}
154175

155176
/**
156177
* @psalm-suppress all
178+
*
179+
* @throws Exception
157180
*/
158181
private function translateLocalTime(string $value): LocalTime
159182
{
160-
$date = new DateTimeImmutable($value);
161-
$timestamp = $date->getTimestamp();
183+
$timestamp = (new DateTimeImmutable($value))->getTimestamp();
162184

163185
$hours = (int) date('H', $timestamp);
164186
$minutes = (int) date('i', $timestamp);

0 commit comments

Comments
 (0)