mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-11 18:19:23 +03:00
Scalar type serialize
method now throws InvariantViolation
and parseValue
throws UserError
This commit is contained in:
parent
8e3d1eb29b
commit
fbcd20814a
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
|
|
||||||
|
use GraphQL\Error\InvariantViolation;
|
||||||
use GraphQL\Error\UserError;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Language\AST\FloatValueNode;
|
use GraphQL\Language\AST\FloatValueNode;
|
||||||
use GraphQL\Language\AST\IntValueNode;
|
use GraphQL\Language\AST\IntValueNode;
|
||||||
@ -31,7 +32,7 @@ values as specified by
|
|||||||
*/
|
*/
|
||||||
public function serialize($value)
|
public function serialize($value)
|
||||||
{
|
{
|
||||||
return $this->coerceFloat($value);
|
return $this->coerceFloat($value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,24 +41,29 @@ values as specified by
|
|||||||
*/
|
*/
|
||||||
public function parseValue($value)
|
public function parseValue($value)
|
||||||
{
|
{
|
||||||
return $this->coerceFloat($value);
|
return $this->coerceFloat($value, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param mixed $value
|
||||||
|
* @param bool $isInput
|
||||||
* @return float|null
|
* @return float|null
|
||||||
*/
|
*/
|
||||||
private function coerceFloat($value)
|
private function coerceFloat($value, $isInput)
|
||||||
{
|
{
|
||||||
if ($value === '') {
|
|
||||||
throw new UserError(
|
|
||||||
'Float cannot represent non numeric value: (empty string)'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (is_numeric($value) || $value === true || $value === false) {
|
if (is_numeric($value) || $value === true || $value === false) {
|
||||||
return (float) $value;
|
return (float) $value;
|
||||||
}
|
}
|
||||||
throw new UserError(sprintf('Float cannot represent non numeric value: %s', Utils::printSafe($value)));
|
|
||||||
|
if ($value === '') {
|
||||||
|
$err = 'Float cannot represent non numeric value: (empty string)';
|
||||||
|
} else {
|
||||||
|
$err = sprintf(
|
||||||
|
'Float cannot represent non numeric value: %s',
|
||||||
|
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw ($isInput ? new UserError($err) : new InvariantViolation($err));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
|
|
||||||
|
use GraphQL\Error\InvariantViolation;
|
||||||
use GraphQL\Error\UserError;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Language\AST\IntValueNode;
|
use GraphQL\Language\AST\IntValueNode;
|
||||||
use GraphQL\Language\AST\StringValueNode;
|
use GraphQL\Language\AST\StringValueNode;
|
||||||
use GraphQL\Utils;
|
use GraphQL\Utils\Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class IDType
|
* Class IDType
|
||||||
@ -33,7 +34,19 @@ When expected as an input type, any string (such as `"4"`) or integer
|
|||||||
*/
|
*/
|
||||||
public function serialize($value)
|
public function serialize($value)
|
||||||
{
|
{
|
||||||
return $this->parseValue($value);
|
if ($value === true) {
|
||||||
|
return 'true';
|
||||||
|
}
|
||||||
|
if ($value === false) {
|
||||||
|
return 'false';
|
||||||
|
}
|
||||||
|
if ($value === null) {
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
if (!is_scalar($value)) {
|
||||||
|
throw new InvariantViolation("ID type cannot represent non scalar value: " . Utils::printSafe($value));
|
||||||
|
}
|
||||||
|
return (string) $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,7 +65,7 @@ When expected as an input type, any string (such as `"4"`) or integer
|
|||||||
return 'null';
|
return 'null';
|
||||||
}
|
}
|
||||||
if (!is_scalar($value)) {
|
if (!is_scalar($value)) {
|
||||||
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
|
throw new UserError("ID type cannot represent non scalar value: " . Utils::printSafeJson($value));
|
||||||
}
|
}
|
||||||
return (string) $value;
|
return (string) $value;
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
*/
|
*/
|
||||||
public function serialize($value)
|
public function serialize($value)
|
||||||
{
|
{
|
||||||
return $this->coerceInt($value);
|
return $this->coerceInt($value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,21 +47,20 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
*/
|
*/
|
||||||
public function parseValue($value)
|
public function parseValue($value)
|
||||||
{
|
{
|
||||||
try {
|
return $this->coerceInt($value, true);
|
||||||
return $this->coerceInt($value);
|
|
||||||
} catch (InvariantViolation $e) {
|
|
||||||
throw new UserError($e->getMessage(), $e->getCode(), $e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
|
* @param bool $isInput
|
||||||
* @return int|null
|
* @return int|null
|
||||||
*/
|
*/
|
||||||
private function coerceInt($value)
|
private function coerceInt($value, $isInput)
|
||||||
{
|
{
|
||||||
|
$errClass = $isInput ? UserError::class : InvariantViolation::class;
|
||||||
|
|
||||||
if ($value === '') {
|
if ($value === '') {
|
||||||
throw new InvariantViolation(
|
throw new $errClass(
|
||||||
'Int cannot represent non 32-bit signed integer value: (empty string)'
|
'Int cannot represent non 32-bit signed integer value: (empty string)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -69,9 +68,10 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
return (int) $value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
if (!is_numeric($value) || $value > self::MAX_INT || $value < self::MIN_INT) {
|
if (!is_numeric($value) || $value > self::MAX_INT || $value < self::MIN_INT) {
|
||||||
throw new InvariantViolation(
|
throw new $errClass(sprintf(
|
||||||
sprintf('Int cannot represent non 32-bit signed integer value: %s', Utils::printSafe($value))
|
'Int cannot represent non 32-bit signed integer value: %s',
|
||||||
);
|
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
$num = (float) $value;
|
$num = (float) $value;
|
||||||
|
|
||||||
@ -82,9 +82,10 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
// Additionally account for scientific notation (i.e. 1e3), because (float)'1e3' is 1000, but (int)'1e3' is 1
|
// Additionally account for scientific notation (i.e. 1e3), because (float)'1e3' is 1000, but (int)'1e3' is 1
|
||||||
$trimmed = floor($num);
|
$trimmed = floor($num);
|
||||||
if ($trimmed !== $num) {
|
if ($trimmed !== $num) {
|
||||||
throw new InvariantViolation(
|
throw new $errClass(sprintf(
|
||||||
'Int cannot represent non-integer value: ' . Utils::printSafe($value)
|
'Int cannot represent non-integer value: %s',
|
||||||
);
|
$isInput ? Utils::printSafeJson($value) : Utils::printSafe($value)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (int) $value;
|
return (int) $value;
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
|
|
||||||
|
use GraphQL\Error\InvariantViolation;
|
||||||
use GraphQL\Error\UserError;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Language\AST\StringValueNode;
|
use GraphQL\Language\AST\StringValueNode;
|
||||||
use GraphQL\Utils;
|
use GraphQL\Utils\Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class StringType
|
* Class StringType
|
||||||
@ -30,7 +31,19 @@ represent free-form human-readable text.';
|
|||||||
*/
|
*/
|
||||||
public function serialize($value)
|
public function serialize($value)
|
||||||
{
|
{
|
||||||
return $this->parseValue($value);
|
if ($value === true) {
|
||||||
|
return 'true';
|
||||||
|
}
|
||||||
|
if ($value === false) {
|
||||||
|
return 'false';
|
||||||
|
}
|
||||||
|
if ($value === null) {
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
if (!is_scalar($value)) {
|
||||||
|
throw new InvariantViolation("String cannot represent non scalar value: " . Utils::printSafe($value));
|
||||||
|
}
|
||||||
|
return (string) $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user