Skip to content

Commit d127002

Browse files
committed
Refactor string checks into utils
1 parent 69786f7 commit d127002

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/Regex.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use GraphQL\Error\Error;
88
use GraphQL\Error\InvariantViolation;
99
use GraphQL\Language\AST\Node;
10-
use GraphQL\Language\AST\StringValueNode;
1110
use GraphQL\Type\Definition\ScalarType;
1211
use GraphQL\Utils\Utils;
1312
use Spatie\Regex\Regex as RegexValidator;
@@ -60,13 +59,7 @@ protected function regex(): string
6059
*/
6160
public function serialize($value): string
6261
{
63-
if (!canBeString($value)) {
64-
$safeValue = Utils::printSafe($value);
65-
66-
throw new InvariantViolation("The given value {$safeValue} can not be serialized.");
67-
}
68-
69-
$stringValue = strval($value);
62+
$stringValue = assertString($value, InvariantViolation::class);
7063

7164
if (!$this->matchesRegex($stringValue)) {
7265
throw new InvariantViolation("The given string $stringValue did not match the regex {$this->regex()}");
@@ -86,14 +79,8 @@ public function serialize($value): string
8679
*/
8780
public function parseValue($value): string
8881
{
89-
if (!canBeString($value)) {
90-
$safeValue = Utils::printSafe($value);
91-
92-
throw new Error("The given value {$safeValue} can not be serialized.");
93-
}
94-
95-
$stringValue = strval($value);
96-
82+
$stringValue = assertString($value, Error::class);
83+
9784
if (!$this->matchesRegex($stringValue)) {
9885
$safeValue = Utils::printSafeJson($stringValue);
9986

@@ -120,12 +107,8 @@ public function parseValue($value): string
120107
*/
121108
public function parseLiteral($valueNode, array $variables = null): string
122109
{
123-
if (!$valueNode instanceof StringValueNode) {
124-
throw new Error("Query error: Can only parse strings got: {$valueNode->kind}", [$valueNode]);
125-
}
126-
127-
$value = $valueNode->value;
128-
110+
$value = assertStringLiteral($valueNode);
111+
129112
if (!$this->matchesRegex($value)) {
130113
$safeValue = Utils::printSafeJson($value);
131114

src/utils.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace MLL\GraphQLScalars;
66

7+
use GraphQL\Error\Error;
8+
use GraphQL\Language\AST\StringValueNode;
9+
use GraphQL\Utils\Utils;
10+
711
/**
812
* Check if a value can be serialized to a string.
913
*
@@ -17,3 +21,37 @@ function canBeString($value): bool
1721
|| is_scalar($value)
1822
|| (is_object($value) && method_exists($value, '__toString'));
1923
}
24+
25+
/**
26+
* @param $valueNode
27+
*
28+
* @return string
29+
* @throws Error
30+
*/
31+
function assertStringLiteral($valueNode): string
32+
{
33+
if (!$valueNode instanceof StringValueNode) {
34+
throw new Error("Query error: Can only parse strings got: {$valueNode->kind}", [$valueNode]);
35+
}
36+
37+
return $valueNode->value;
38+
}
39+
40+
/**
41+
* Ensure the value is a string and throw an exception if not.
42+
*
43+
* @param $value
44+
* @param string $exceptionClass
45+
*
46+
* @return string
47+
*/
48+
function assertString($value, string $exceptionClass): string
49+
{
50+
if (!canBeString($value)) {
51+
$safeValue = Utils::printSafe($value);
52+
53+
throw new $exceptionClass("The given value {$safeValue} can not be serialized.");
54+
}
55+
56+
return strval($value);
57+
}

0 commit comments

Comments
 (0)