7
7
use GraphQL \Error \Error ;
8
8
use GraphQL \Error \InvariantViolation ;
9
9
use GraphQL \Language \AST \Node ;
10
- use GraphQL \Language \AST \StringValueNode ;
11
10
use GraphQL \Type \Definition \ScalarType ;
12
11
use GraphQL \Utils \Utils ;
13
12
use Spatie \Regex \Regex as RegexValidator ;
@@ -60,13 +59,7 @@ protected function regex(): string
60
59
*/
61
60
public function serialize ($ value ): string
62
61
{
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);
70
63
71
64
if (!$ this ->matchesRegex ($ stringValue )) {
72
65
throw new InvariantViolation ("The given string $ stringValue did not match the regex {$ this ->regex ()}" );
@@ -86,14 +79,8 @@ public function serialize($value): string
86
79
*/
87
80
public function parseValue ($ value ): string
88
81
{
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
+
97
84
if (!$ this ->matchesRegex ($ stringValue )) {
98
85
$ safeValue = Utils::printSafeJson ($ stringValue );
99
86
@@ -120,12 +107,8 @@ public function parseValue($value): string
120
107
*/
121
108
public function parseLiteral ($ valueNode , array $ variables = null ): string
122
109
{
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
+
129
112
if (!$ this ->matchesRegex ($ value )) {
130
113
$ safeValue = Utils::printSafeJson ($ value );
131
114
0 commit comments