4
4
5
5
namespace MLL \GraphQLScalars ;
6
6
7
+ use GraphQL \Error \Error ;
7
8
use GraphQL \Error \InvariantViolation ;
8
9
use GraphQL \Language \AST \Node ;
10
+ use GraphQL \Language \AST \StringValueNode ;
9
11
use GraphQL \Type \Definition \ScalarType ;
12
+ use GraphQL \Utils \Utils ;
10
13
use Spatie \Regex \Regex as RegexValidator ;
11
14
12
15
abstract class Regex extends ScalarType
13
16
{
17
+ /**
18
+ * Return the Regex that the values are validated against.
19
+ *
20
+ * @return string
21
+ */
22
+ abstract protected function regex (): string ;
23
+
24
+ /**
25
+ * This factory method allows you to create a Regex scalar in a one-liner.
26
+ *
27
+ * @param string $name
28
+ * @param string $regex
29
+ *
30
+ * @return Regex
31
+ */
32
+ public static function make (string $ name , string $ regex ): Regex
33
+ {
34
+ $ regexClass = new class () extends Regex {
35
+ /**
36
+ * Return the Regex that the values are validated against.
37
+ *
38
+ * Must be a valid
39
+ *
40
+ * @return string
41
+ */
42
+ protected function regex (): string
43
+ {
44
+ return $ this ->regex ;
45
+ }
46
+ };
47
+
48
+ $ regexClass ->name = $ name ;
49
+ $ regexClass ->regex = $ regex ;
50
+
51
+ return $ regexClass ;
52
+ }
53
+
14
54
/**
15
55
* Serializes an internal value to include in a response.
16
56
*
17
- * @param string $value
57
+ * @param mixed $value
18
58
*
19
59
* @return string
20
60
*/
21
61
public function serialize ($ value ): string
22
62
{
23
63
if (!canBeString ($ value )) {
24
- $ valueClass = get_class ($ value );
64
+ $ safeValue = Utils:: printSafe ($ value );
25
65
26
- throw new InvariantViolation ("The given value of class $ valueClass can not be serialized. " );
66
+ throw new InvariantViolation ("The given value { $ safeValue } can not be serialized. " );
27
67
}
28
68
29
69
$ stringValue = strval ($ value );
@@ -34,21 +74,34 @@ public function serialize($value): string
34
74
35
75
return $ stringValue ;
36
76
}
37
-
77
+
38
78
/**
39
79
* Parses an externally provided value (query variable) to use as an input.
40
80
*
41
81
* @param mixed $value
42
82
*
83
+ * @throws Error
84
+ *
43
85
* @return mixed
44
86
*/
45
- public function parseValue ($ value )
87
+ public function parseValue ($ value ): string
46
88
{
47
- // TODO implement validation
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
+
97
+ if (!$ this ->matchesRegex ($ stringValue )){
98
+ $ safeValue = Utils::printSafeJson ($ stringValue );
99
+ throw new Error ("The given value {$ safeValue } did not match the regex {$ this ->regex ()}" );
100
+ }
48
101
49
102
return $ value ;
50
103
}
51
-
104
+
52
105
/**
53
106
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
54
107
*
@@ -60,13 +113,24 @@ public function parseValue($value)
60
113
* @param Node $valueNode
61
114
* @param array $variables
62
115
*
63
- * @return mixed
116
+ * @throws Error
117
+ *
118
+ * @return string
64
119
*/
65
- public function parseLiteral ($ valueNode , array $ variables = null )
120
+ public function parseLiteral ($ valueNode , array $ variables = null ): string
66
121
{
67
- // TODO implement validation
68
-
69
- return $ valueNode ->value ;
122
+ if (!$ valueNode instanceof StringValueNode) {
123
+ throw new Error ("Query error: Can only parse strings got: {$ valueNode ->kind }" , [$ valueNode ]);
124
+ }
125
+
126
+ $ value = $ valueNode ->value ;
127
+
128
+ if (!$ this ->matchesRegex ($ value )){
129
+ $ safeValue = Utils::printSafeJson ($ value );
130
+ throw new Error ("The given value {$ safeValue } did not match the regex {$ this ->regex ()}" , [$ valueNode ]);
131
+ }
132
+
133
+ return $ value ;
70
134
}
71
135
72
136
/**
@@ -81,13 +145,4 @@ protected function matchesRegex(string $value): bool
81
145
$ value
82
146
)->hasMatch ();
83
147
}
84
-
85
- /**
86
- * Return the Regex that the values are validated against.
87
- *
88
- * Must be a valid
89
- *
90
- * @return string
91
- */
92
- abstract protected function regex (): string ;
93
148
}
0 commit comments