Skip to content

Commit 9c32917

Browse files
committed
Simplify parseLiteral to call parseValue
1 parent cfb37cf commit 9c32917

10 files changed

+11
-99
lines changed

src/Type/Date.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $valueNode->value)) {
34-
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
35-
}
36-
37-
return DateTime::createFromFormat(DateTime::ATOM, $valueNode->value . 'T00:00:00+00:00');
33+
return $this->parseValue($valueNode->value);
3834
}
3935

4036
public function parseValue(mixed $value): DateTime

src/Type/DateImmutable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $valueNode->value)) {
34-
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
35-
}
36-
37-
return DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $valueNode->value . 'T00:00:00+00:00');
33+
return $this->parseValue($valueNode->value);
3834
}
3935

4036
public function parseValue(mixed $value): DateTimeImmutable|false

src/Type/DateTime.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DateTime extends ScalarType
2121
public string|null $description = 'The `datetime` scalar type represents datetime data.'
2222
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00.';
2323

24-
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTime|null
24+
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTime
2525
{
2626
// @codeCoverageIgnoreStart
2727
if (! $valueNode instanceof StringValueNode) {
@@ -30,17 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! $valueNode->value) {
34-
return null;
35-
}
36-
37-
$data = PHPDateTime::createFromFormat(PHPDateTime::ATOM, $valueNode->value);
38-
39-
if ($data === false) {
40-
throw new Error('datetime format does not match ISO 8601.');
41-
}
42-
43-
return $data;
33+
return $this->parseValue($valueNode->value);
4434
}
4535

4636
public function parseValue(mixed $value): PHPDateTime

src/Type/DateTimeImmutable.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DateTimeImmutable extends ScalarType
2121
public string|null $description = 'The `datetime_immutable` scalar type represents datetime data.'
2222
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00';
2323

24-
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeImmutable|null
24+
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeImmutable
2525
{
2626
// @codeCoverageIgnoreStart
2727
if (! $valueNode instanceof StringValueNode) {
@@ -30,17 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! $valueNode->value) {
34-
return null;
35-
}
36-
37-
$data = PHPDateTimeImmutable::createFromFormat(PHPDateTimeImmutable::ATOM, $valueNode->value);
38-
39-
if ($data === false) {
40-
throw new Error('datetime format does not match ISO 8601.');
41-
}
42-
43-
return $data;
33+
return $this->parseValue($valueNode->value);
4434
}
4535

4636
public function parseValue(mixed $value): PHPDateTimeImmutable

src/Type/DateTimeTZ.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DateTimeTZ extends ScalarType
2121
public string|null $description = 'The `datetimetz` scalar type represents datetime data.'
2222
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00.';
2323

24-
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeTZ|null
24+
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeTZ
2525
{
2626
// @codeCoverageIgnoreStart
2727
if (! $valueNode instanceof StringValueNode) {
@@ -30,17 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! $valueNode->value) {
34-
return null;
35-
}
36-
37-
$data = PHPDateTimeTZ::createFromFormat(PHPDateTimeTZ::ATOM, $valueNode->value);
38-
39-
if ($data === false) {
40-
throw new Error('datetimetz format does not match ISO 8601.');
41-
}
42-
43-
return $data;
33+
return $this->parseValue($valueNode->value);
4434
}
4535

4636
public function parseValue(mixed $value): PHPDateTimeTZ

src/Type/DateTimeTZImmutable.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DateTimeTZImmutable extends ScalarType
2121
public string|null $description = 'The `datetimetz_immutable` scalar type represents datetime data.'
2222
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00';
2323

24-
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeTZImmutable|null
24+
public function parseLiteral(ASTNode $valueNode, array|null $variables = null): PHPDateTimeTZImmutable
2525
{
2626
// @codeCoverageIgnoreStart
2727
if (! $valueNode instanceof StringValueNode) {
@@ -30,20 +30,10 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):
3030

3131
// @codeCoverageIgnoreEnd
3232

33-
if (! $valueNode->value) {
34-
return null;
35-
}
36-
37-
$data = PHPDateTimeTZImmutable::createFromFormat(PHPDateTimeTZImmutable::ATOM, $valueNode->value);
38-
39-
if ($data === false) {
40-
throw new Error('datetimetz_immutable format does not match ISO 8601. ' . $valueNode->value);
41-
}
42-
43-
return $data;
33+
return $this->parseValue($valueNode->value);
4434
}
4535

46-
public function parseValue(mixed $value): PHPDateTimeTZImmutable|false
36+
public function parseValue(mixed $value): PHPDateTimeTZImmutable
4737
{
4838
if (! is_string($value)) {
4939
throw new Error('datetimetz_immutable is not a string: ' . $value);

test/Feature/Type/DateTimeImmutableTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
4545
$result = $dateType->parseValue('2023-11-26');
4646
}
4747

48-
public function testParseLiteralNull(): void
49-
{
50-
$dateTimeType = new DateTimeImmutable();
51-
$node = new StringValueNode([]);
52-
$node->value = '';
53-
$result = $dateTimeType->parseLiteral($node);
54-
55-
$this->AssertNull($result);
56-
}
57-
5848
public function testParseLiteralInvalid(): void
5949
{
6050
$this->expectException(Error::class);

test/Feature/Type/DateTimeTZImmutableTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
4545
$result = $dateType->parseValue('03/01/2020');
4646
}
4747

48-
public function testParseLiteralNull(): void
49-
{
50-
$dateTimeType = new DateTimeType();
51-
$node = new StringValueNode([]);
52-
$node->value = '';
53-
$result = $dateTimeType->parseLiteral($node);
54-
55-
$this->AssertNull($result);
56-
}
57-
5848
public function testParseLiteralInvalid(): void
5949
{
6050
$this->expectException(Error::class);

test/Feature/Type/DateTimeTZTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ public function testParseValueInvalid(): void
4343
$result = $dateType->parseValue('03/01/2020');
4444
}
4545

46-
public function testParseLiteralNull(): void
47-
{
48-
$dateTimeType = new DateTimeType();
49-
$node = new StringValueNode([]);
50-
$node->value = '';
51-
$result = $dateTimeType->parseLiteral($node);
52-
53-
$this->AssertNull($result);
54-
}
55-
5646
public function testParseLiteralInvalid(): void
5747
{
5848
$this->expectException(Error::class);

test/Feature/Type/DateTimeTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
4545
$result = $dateType->parseValue('03/01/2020');
4646
}
4747

48-
public function testParseLiteralNull(): void
49-
{
50-
$dateTimeType = new DateTimeType();
51-
$node = new StringValueNode([]);
52-
$node->value = '';
53-
$result = $dateTimeType->parseLiteral($node);
54-
55-
$this->AssertNull($result);
56-
}
57-
5848
public function testParseLiteralInvalid(): void
5949
{
6050
$this->expectException(Error::class);

0 commit comments

Comments
 (0)