-
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
169 additions
and
27 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,59 @@ | ||
<?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 function min(): int; | ||
|
||
/** The maximum allowed value. */ | ||
abstract protected function max(): int; | ||
|
||
public ?string $description = 'Checks if the given column is of the format 96-well column'; | ||
|
||
public function serialize($value) | ||
{ | ||
if (is_int($value) && $this->isValueInExpectedRange($value)) { | ||
return $value; | ||
} | ||
|
||
$notInRange = Utils::printSafe($value); | ||
throw new \InvalidArgumentException("Value not in range: {$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: {$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: {$notInRange}.", $valueNode); | ||
} | ||
|
||
private function isValueInExpectedRange(int $value): bool | ||
{ | ||
return $value <= static::max() && $value >= static::min(); | ||
} | ||
} |
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: "12".'); | ||
|
||
(new UpToADozen())->serialize('12'); | ||
} | ||
|
||
public function testSerializeThrowsIfInvalid(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Value not in range: 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: 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 function min(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected function max(): int | ||
{ | ||
return 12; | ||
} | ||
} |