Skip to content

Commit a69fc9d

Browse files
committed
Update to webonyx/graphql-php 0.13 and PHP 7.1+
1 parent adde60a commit a69fc9d

15 files changed

+234
-101
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.0
54
- 7.1
65
- 7.2
76

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ use MLL\GraphQLScalars\Regex;
6464
// The name is implicitly set through the class name here
6565
class HexValue extends Regex
6666
{
67-
public $description = 'A hexadecimal color is specified with: #RRGGBB, where RR (red), GG (green) and BB (blue) are hexadecimal integers between 00 and FF specifying the intensity of the color.';
68-
69-
protected function regex() : string
67+
/**
68+
* The description that is used for schema introspection.
69+
*
70+
* @var string
71+
*/
72+
public $description = <<<'DESCRIPTION'
73+
A hexadecimal color is specified with: #RRGGBB, where RR (red), GG (green) and BB (blue)
74+
are hexadecimal integers between 00 and FF specifying the intensity of the color.
75+
DESCRIPTION;
76+
77+
public static function regex() : string
7078
{
7179
return '/^#?([a-f0-9]{6}|[a-f0-9]{3})$/';
7280
}
@@ -75,7 +83,7 @@ class HexValue extends Regex
7583

7684
### [StringScalar](StringScalar.php)
7785

78-
The `StringScalar` encapsulate all the boilerplate associated with creating a string-based Scalar type.
86+
The `StringScalar` encapsulates all the boilerplate associated with creating a string-based Scalar type.
7987
It does the proper string checking for you and let's you focus on the minimal logic that is specific to your use case.
8088

8189
All you have to specify is a function that checks if the given string is valid. Use the factory method

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.0",
15-
"webonyx/graphql-php": "^0.12.6",
14+
"php": ">=7.1",
15+
"webonyx/graphql-php": "^0.13",
1616
"spatie/regex": "^1.3",
1717
"egulias/email-validator": "^2.1"
1818
},
1919
"require-dev": {
2020
"ext-json": "*",
21-
"phpunit/phpunit": "^6|^7.3",
21+
"phpunit/phpunit": "^7.3",
2222
"psy/psysh": "^0.9.8"
2323
},
2424
"autoload": {

src/Email.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
class Email extends StringScalar
1111
{
12-
/** @var string */
12+
/**
13+
* The description that is used for schema introspection.
14+
*
15+
* @var string
16+
*/
1317
public $description = 'A RFC 5321 compliant email.';
1418

1519
/**

src/Mixed.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010

1111
class Mixed extends ScalarType
1212
{
13-
/** @var string */
14-
public $description = <<<'EOT'
13+
/**
14+
* The description that is used for schema introspection.
15+
*
16+
* @var string
17+
*/
18+
public $description = <<<'DESCRIPTION'
1519
Loose type that allows any value. Be careful when passing in large Int or Float literals,
1620
as they may not be parsed correctly on the server side. Use String literals if you are
1721
dealing with really large numbers to be on the safe side.
18-
EOT;
22+
DESCRIPTION;
1923

2024
/**
2125
* Serializes an internal value to include in a response.
@@ -46,16 +50,16 @@ public function parseValue($value)
4650
/**
4751
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
4852
*
49-
* In the case of an invalid node or value this method must throw an Exception
53+
* In the case of an invalid node or value, this method must throw an Exception.
5054
*
5155
* @param ValueNode $valueNode
52-
* @param array|null $variables
56+
* @param \mixed[]|null $variables
5357
*
5458
* @throws \Exception
5559
*
5660
* @return \mixed
5761
*/
58-
public function parseLiteral($valueNode, array $variables = null)
62+
public function parseLiteral($valueNode, ?array $variables = null)
5963
{
6064
return AST::valueFromASTUntyped($valueNode);
6165
}

src/Regex.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ abstract class Regex extends ScalarType
2020
* @param string|null $description A description for the type.
2121
* @param string $regex The regular expression that is validated against.
2222
*
23-
* @return Regex
23+
* @return static
2424
*/
25-
public static function make(string $name, string $description = null, string $regex): self
25+
public static function make(string $name, ?string $description = null, string $regex): self
2626
{
27-
$regexClass = new class() extends Regex {
28-
/** @var string Is set dynamically during this class creation. */
27+
$concreteRegex = new class() extends Regex {
28+
/**
29+
* The regex that values are validated against.
30+
*
31+
* Is set dynamically during this class creation.
32+
*
33+
* @var string
34+
*/
2935
public static $regex;
3036

3137
/**
@@ -41,11 +47,11 @@ public static function regex(): string
4147
}
4248
};
4349

44-
$regexClass->name = $name;
45-
$regexClass->description = $description;
46-
$regexClass::$regex = $regex;
50+
$concreteRegex->name = $name;
51+
$concreteRegex->description = $description;
52+
$concreteRegex::$regex = $regex;
4753

48-
return $regexClass;
54+
return $concreteRegex;
4955
}
5056

5157
/**
@@ -64,7 +70,7 @@ abstract public static function regex(): string;
6470
*/
6571
public function serialize($value): string
6672
{
67-
$stringValue = assertString($value, InvariantViolation::class);
73+
$stringValue = coerceToString($value, InvariantViolation::class);
6874

6975
if (!static::matchesRegex($stringValue)) {
7076
throw new InvariantViolation(
@@ -76,6 +82,8 @@ public function serialize($value): string
7682
}
7783

7884
/**
85+
* Determine if the given string matches the regex defined in this class.
86+
*
7987
* @param string $value
8088
*
8189
* @return bool
@@ -99,7 +107,7 @@ protected static function matchesRegex(string $value): bool
99107
*/
100108
public function parseValue($value): string
101109
{
102-
$stringValue = assertString($value, Error::class);
110+
$stringValue = coerceToString($value, Error::class);
103111

104112
if (!static::matchesRegex($stringValue)) {
105113
throw new Error(
@@ -114,15 +122,15 @@ public function parseValue($value): string
114122
* Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
115123
*
116124
* @param Node $valueNode
117-
* @param array $variables
125+
* @param mixed[]|null $variables
118126
*
119127
* @throws Error
120128
*
121129
* @return string
122130
*/
123-
public function parseLiteral($valueNode, array $variables = null): string
131+
public function parseLiteral($valueNode, ?array $variables = null): string
124132
{
125-
$value = assertStringLiteral($valueNode);
133+
$value = extractStringFromLiteral($valueNode);
126134

127135
if (!static::matchesRegex($value)) {
128136
throw new Error(
@@ -135,6 +143,8 @@ public function parseLiteral($valueNode, array $variables = null): string
135143
}
136144

137145
/**
146+
* Construct the error message that occurs when the given string does not match the regex.
147+
*
138148
* @param string $value
139149
*
140150
* @return string

src/StringScalar.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
abstract class StringScalar extends ScalarType
1414
{
1515
/**
16+
* Instantiate an anonymous subclass that can be used in a schema.
17+
*
1618
* @param string $name The name that the scalar type will have in the schema.
1719
* @param string|null $description A description for the type.
1820
* @param callable $isValid A function that returns a boolean whether a given string is valid.
1921
*
20-
* @return StringScalar
22+
* @return static
2123
*/
22-
public static function make(string $name, string $description = null, callable $isValid): self
24+
public static function make(string $name, ?string $description = null, callable $isValid): self
2325
{
24-
$instance = new class() extends StringScalar {
26+
$concreteStringScalar = new class() extends StringScalar {
2527
/**
2628
* Check if the given string is a valid email.
2729
*
@@ -35,15 +37,15 @@ protected function isValid(string $stringValue): bool
3537
}
3638
};
3739

38-
$instance->name = $name;
39-
$instance->description = $description;
40-
$instance->isValid = $isValid;
40+
$concreteStringScalar->name = $name;
41+
$concreteStringScalar->description = $description;
42+
$concreteStringScalar->isValid = $isValid;
4143

42-
return $instance;
44+
return $concreteStringScalar;
4345
}
4446

4547
/**
46-
* Check if the given string is a valid email.
48+
* Check if the given string is valid.
4749
*
4850
* @param string $stringValue
4951
*
@@ -54,13 +56,15 @@ abstract protected function isValid(string $stringValue): bool;
5456
/**
5557
* Serializes an internal value to include in a response.
5658
*
57-
* @param string $value
59+
* @param mixed $value
60+
*
61+
* @throws InvariantViolation
5862
*
5963
* @return string
6064
*/
6165
public function serialize($value): string
6266
{
63-
$stringValue = assertString($value, InvariantViolation::class);
67+
$stringValue = coerceToString($value, InvariantViolation::class);
6468

6569
if (!$this->isValid($stringValue)) {
6670
throw new InvariantViolation(
@@ -72,6 +76,8 @@ public function serialize($value): string
7276
}
7377

7478
/**
79+
* Construct an error message that occurs when an invalid string is passed.
80+
*
7581
* @param string $stringValue
7682
*
7783
* @return string
@@ -94,7 +100,7 @@ public function invalidStringMessage(string $stringValue): string
94100
*/
95101
public function parseValue($value): string
96102
{
97-
$stringValue = assertString($value, Error::class);
103+
$stringValue = coerceToString($value, Error::class);
98104

99105
if (!$this->isValid($stringValue)) {
100106
throw new Error(
@@ -114,15 +120,15 @@ public function parseValue($value): string
114120
* }
115121
*
116122
* @param Node $valueNode
117-
* @param array $variables
123+
* @param mixed[]|null $variables
118124
*
119125
* @throws Error
120126
*
121127
* @return string
122128
*/
123-
public function parseLiteral($valueNode, array $variables = null): string
129+
public function parseLiteral($valueNode, ?array $variables = null): string
124130
{
125-
$stringValue = assertStringLiteral($valueNode);
131+
$stringValue = extractStringFromLiteral($valueNode);
126132

127133
if (!$this->isValid($stringValue)) {
128134
throw new Error(

src/utils.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Check if a value can be serialized to a string.
1313
*
14-
* @param $value
14+
* @param mixed $value
1515
*
1616
* @return bool
1717
*/
@@ -23,35 +23,43 @@ function canBeString($value): bool
2323
}
2424

2525
/**
26-
* @param $valueNode
26+
* Get the underlying string from a GraphQL literal and throw if Literal is not a string.
27+
*
28+
* @param mixed $valueNode
2729
*
2830
* @throws Error
2931
*
3032
* @return string
3133
*/
32-
function assertStringLiteral($valueNode): string
34+
function extractStringFromLiteral($valueNode): string
3335
{
3436
if (!$valueNode instanceof StringValueNode) {
35-
throw new Error("Query error: Can only parse strings got: {$valueNode->kind}", [$valueNode]);
37+
throw new Error(
38+
"Query error: Can only parse strings got: {$valueNode->kind}", [$valueNode]
39+
);
3640
}
3741

3842
return $valueNode->value;
3943
}
4044

4145
/**
42-
* Ensure the value is a string and throw an exception if not.
46+
* Convert the value to a string and throw an exception if it is not possible.
4347
*
44-
* @param $value
48+
* @param mixed $value
4549
* @param string $exceptionClass
4650
*
51+
* @throws <$exceptionClass>
52+
*
4753
* @return string
4854
*/
49-
function assertString($value, string $exceptionClass): string
55+
function coerceToString($value, string $exceptionClass): string
5056
{
5157
if (!canBeString($value)) {
5258
$safeValue = Utils::printSafeJson($value);
5359

54-
throw new $exceptionClass("The given value {$safeValue} can not be serialized.");
60+
throw new $exceptionClass(
61+
"The given value {$safeValue} can not be serialized."
62+
);
5563
}
5664

5765
return strval($value);

0 commit comments

Comments
 (0)