-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars; | ||
|
||
use GraphQL\Error\Error; | ||
use GraphQL\Language\AST\IntValueNode; | ||
use GraphQL\Language\AST\Node; | ||
use GraphQL\Language\Printer; | ||
use GraphQL\Type\Definition\ScalarType; | ||
use GraphQL\Utils\Utils; | ||
|
||
abstract class IntRange extends ScalarType | ||
{ | ||
/** The minimum allowed value. */ | ||
abstract protected static function min(): int; | ||
|
||
/** The maximum allowed value. */ | ||
abstract protected static function max(): int; | ||
|
||
public function serialize($value) | ||
{ | ||
if (is_int($value) && $this->isValueInExpectedRange($value)) { | ||
return $value; | ||
} | ||
|
||
$notInRange = Utils::printSafe($value); | ||
throw new \InvalidArgumentException("Value not in range {$this->rangeDescription()}: {$notInRange}."); | ||
} | ||
|
||
public function parseValue($value) | ||
{ | ||
if (is_int($value) && $this->isValueInExpectedRange($value)) { | ||
return $value; | ||
} | ||
|
||
$notInRange = Utils::printSafe($value); | ||
throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}."); | ||
} | ||
|
||
public function parseLiteral(Node $valueNode, ?array $variables = null) | ||
{ | ||
if ($valueNode instanceof IntValueNode) { | ||
$value = (int) $valueNode->value; | ||
if ($this->isValueInExpectedRange($value)) { | ||
return $value; | ||
} | ||
} | ||
|
||
$notInRange = Printer::doPrint($valueNode); | ||
throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}.", $valueNode); | ||
} | ||
|
||
private function isValueInExpectedRange(int $value): bool | ||
{ | ||
return $value <= static::max() && $value >= static::min(); | ||
} | ||
|
||
private function rangeDescription(): string | ||
{ | ||
$min = static::min(); | ||
$max = static::max(); | ||
|
||
return "{$min}-{$max}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
use GraphQL\Error\Error; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class IntRangeTest extends TestCase | ||
{ | ||
public function testSerializeThrowsIfNotAnInt(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Value not in range 1-12: "12".'); | ||
|
||
(new UpToADozen())->serialize('12'); | ||
} | ||
|
||
public function testSerializeThrowsIfInvalid(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Value not in range 1-12: 13.'); | ||
|
||
(new UpToADozen())->serialize(13); | ||
} | ||
|
||
public function testSerializePassesWhenValid(): void | ||
{ | ||
$serializedResult = (new UpToADozen())->serialize(12); | ||
|
||
self::assertSame(12, $serializedResult); | ||
} | ||
|
||
public function testParseValueThrowsIfInvalid(): void | ||
{ | ||
$this->expectException(Error::class); | ||
$this->expectExceptionMessage('Value not in range 1-12: 13.'); | ||
|
||
(new UpToADozen())->parseValue(13); | ||
} | ||
|
||
public function testParseValuePassesIfValid(): void | ||
{ | ||
self::assertSame( | ||
12, | ||
(new UpToADozen())->parseValue(12) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\GraphQLScalars\Tests; | ||
|
||
use MLL\GraphQLScalars\IntRange; | ||
|
||
final class UpToADozen extends IntRange | ||
{ | ||
protected static function min(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected static function max(): int | ||
{ | ||
return 12; | ||
} | ||
} |