Skip to content

Commit

Permalink
Simplify parseLiteral to call parseValue
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Oct 18, 2024
1 parent cfb37cf commit 9c32917
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 99 deletions.
6 changes: 1 addition & 5 deletions src/Type/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):

// @codeCoverageIgnoreEnd

if (! preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $valueNode->value)) {
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
}

return DateTime::createFromFormat(DateTime::ATOM, $valueNode->value . 'T00:00:00+00:00');
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): DateTime
Expand Down
6 changes: 1 addition & 5 deletions src/Type/DateImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public function parseLiteral(ASTNode $valueNode, array|null $variables = null):

// @codeCoverageIgnoreEnd

if (! preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $valueNode->value)) {
throw new Error('Date format does not match Y-m-d e.g. 2004-02-12.');
}

return DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $valueNode->value . 'T00:00:00+00:00');
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): DateTimeImmutable|false
Expand Down
14 changes: 2 additions & 12 deletions src/Type/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DateTime extends ScalarType
public string|null $description = 'The `datetime` scalar type represents datetime data.'
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00.';

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

// @codeCoverageIgnoreEnd

if (! $valueNode->value) {
return null;
}

$data = PHPDateTime::createFromFormat(PHPDateTime::ATOM, $valueNode->value);

if ($data === false) {
throw new Error('datetime format does not match ISO 8601.');
}

return $data;
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): PHPDateTime
Expand Down
14 changes: 2 additions & 12 deletions src/Type/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DateTimeImmutable extends ScalarType
public string|null $description = 'The `datetime_immutable` scalar type represents datetime data.'
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00';

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

// @codeCoverageIgnoreEnd

if (! $valueNode->value) {
return null;
}

$data = PHPDateTimeImmutable::createFromFormat(PHPDateTimeImmutable::ATOM, $valueNode->value);

if ($data === false) {
throw new Error('datetime format does not match ISO 8601.');
}

return $data;
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): PHPDateTimeImmutable
Expand Down
14 changes: 2 additions & 12 deletions src/Type/DateTimeTZ.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DateTimeTZ extends ScalarType
public string|null $description = 'The `datetimetz` scalar type represents datetime data.'
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00.';

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

// @codeCoverageIgnoreEnd

if (! $valueNode->value) {
return null;
}

$data = PHPDateTimeTZ::createFromFormat(PHPDateTimeTZ::ATOM, $valueNode->value);

if ($data === false) {
throw new Error('datetimetz format does not match ISO 8601.');
}

return $data;
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): PHPDateTimeTZ
Expand Down
16 changes: 3 additions & 13 deletions src/Type/DateTimeTZImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DateTimeTZImmutable extends ScalarType
public string|null $description = 'The `datetimetz_immutable` scalar type represents datetime data.'
. 'The format is ISO-8601 e.g. 2004-02-12T15:19:21+00:00';

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

// @codeCoverageIgnoreEnd

if (! $valueNode->value) {
return null;
}

$data = PHPDateTimeTZImmutable::createFromFormat(PHPDateTimeTZImmutable::ATOM, $valueNode->value);

if ($data === false) {
throw new Error('datetimetz_immutable format does not match ISO 8601. ' . $valueNode->value);
}

return $data;
return $this->parseValue($valueNode->value);
}

public function parseValue(mixed $value): PHPDateTimeTZImmutable|false
public function parseValue(mixed $value): PHPDateTimeTZImmutable
{
if (! is_string($value)) {
throw new Error('datetimetz_immutable is not a string: ' . $value);
Expand Down
10 changes: 0 additions & 10 deletions test/Feature/Type/DateTimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
$result = $dateType->parseValue('2023-11-26');
}

public function testParseLiteralNull(): void
{
$dateTimeType = new DateTimeImmutable();
$node = new StringValueNode([]);
$node->value = '';
$result = $dateTimeType->parseLiteral($node);

$this->AssertNull($result);
}

public function testParseLiteralInvalid(): void
{
$this->expectException(Error::class);
Expand Down
10 changes: 0 additions & 10 deletions test/Feature/Type/DateTimeTZImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
$result = $dateType->parseValue('03/01/2020');
}

public function testParseLiteralNull(): void
{
$dateTimeType = new DateTimeType();
$node = new StringValueNode([]);
$node->value = '';
$result = $dateTimeType->parseLiteral($node);

$this->AssertNull($result);
}

public function testParseLiteralInvalid(): void
{
$this->expectException(Error::class);
Expand Down
10 changes: 0 additions & 10 deletions test/Feature/Type/DateTimeTZTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ public function testParseValueInvalid(): void
$result = $dateType->parseValue('03/01/2020');
}

public function testParseLiteralNull(): void
{
$dateTimeType = new DateTimeType();
$node = new StringValueNode([]);
$node->value = '';
$result = $dateTimeType->parseLiteral($node);

$this->AssertNull($result);
}

public function testParseLiteralInvalid(): void
{
$this->expectException(Error::class);
Expand Down
10 changes: 0 additions & 10 deletions test/Feature/Type/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public function testParseValueInvalid(): void
$result = $dateType->parseValue('03/01/2020');
}

public function testParseLiteralNull(): void
{
$dateTimeType = new DateTimeType();
$node = new StringValueNode([]);
$node->value = '';
$result = $dateTimeType->parseLiteral($node);

$this->AssertNull($result);
}

public function testParseLiteralInvalid(): void
{
$this->expectException(Error::class);
Expand Down

0 comments on commit 9c32917

Please sign in to comment.