Moved GraphQL\Utils to GraphQL\Utils\Utils

This commit is contained in:
Vladimir Razuvaev 2017-07-10 19:50:26 +07:00
parent ed28deda81
commit 296544089c
66 changed files with 461 additions and 442 deletions

View File

@ -1,5 +1,9 @@
# Upgrade
## Upgrade v0.9.x > v0.10.x
### Deprecated: GraphQL\Utils moved to GraphQL\Utils\Utils
Old class still exists, but triggers deprecation warning.
## Upgrade v0.7.x > v0.8.x
All of those changes apply to those who extends various parts of this library.
If you only use the library and don't try to extend it - everything should work without breaks.

View File

@ -12,7 +12,7 @@ use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class QueryGenerator
{

View File

@ -44,7 +44,7 @@ namespace MyApp;
use GraphQL\Error\Error;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class EmailType extends ScalarType
{

View File

@ -1,9 +1,7 @@
<?php
namespace GraphQL\Examples\Blog;
use GraphQL\Examples\Blog\Data\DataSource;
use GraphQL\Examples\Blog\Data\User;
use GraphQL\Utils;
/**
* Class AppContext

View File

@ -2,7 +2,7 @@
namespace GraphQL\Examples\Blog\Data;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class Comment
{

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Examples\Blog\Data;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class Image
{

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Examples\Blog\Data;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class Story
{

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Examples\Blog\Data;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class User
{

View File

@ -4,7 +4,7 @@ namespace GraphQL\Examples\Blog\Type\Scalar;
use GraphQL\Error\Error;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class EmailType
{

View File

@ -5,7 +5,7 @@ use GraphQL\Error\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class UrlType extends ScalarType
{

View File

@ -3,7 +3,7 @@ namespace GraphQL\Error;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class Error

View File

@ -4,7 +4,7 @@ namespace GraphQL\Error;
use GraphQL\Language\SourceLocation;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class FormattedError
@ -68,7 +68,7 @@ class FormattedError
// Remove invariant entries as they don't provide much value:
if (
isset($trace[0]['function']) && isset($trace[0]['class']) &&
('GraphQL\Utils::invariant' === $trace[0]['class'].'::'.$trace[0]['function'])) {
('GraphQL\Utils\Utils::invariant' === $trace[0]['class'].'::'.$trace[0]['function'])) {
array_shift($trace);
}

View File

@ -27,7 +27,8 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Introspection;
use GraphQL\Utils;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
/**
* Terminology
@ -609,7 +610,7 @@ class Executor
return true;
}
$conditionalType = Utils\TypeInfo::typeFromAST($this->exeContext->schema, $typeConditionNode);
$conditionalType = TypeInfo::typeFromAST($this->exeContext->schema, $typeConditionNode);
if ($conditionalType === $type) {
return true;
}

View File

@ -3,7 +3,7 @@ namespace GraphQL\Executor\Promise\Adapter;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use React\Promise\Promise as ReactPromise;
use React\Promise\PromiseInterface as ReactPromiseInterface;

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Executor\Promise\Adapter;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class SyncPromise

View File

@ -5,7 +5,7 @@ use GraphQL\Deferred;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class SyncPromiseAdapter

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Executor\Promise;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Convenience wrapper for promises represented by Promise Adapter

View File

@ -23,7 +23,9 @@ use GraphQL\Type\Definition\LeafType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\AST;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
use GraphQL\Validator\DocumentValidator;
class Values
@ -44,7 +46,7 @@ class Values
$coercedValues = [];
foreach ($definitionNodes as $definitionNode) {
$varName = $definitionNode->variable->name->value;
$varType = Utils\TypeInfo::typeFromAST($schema, $definitionNode->type);
$varType = TypeInfo::typeFromAST($schema, $definitionNode->type);
if (!Type::isInputType($varType)) {
throw new Error(
@ -57,7 +59,7 @@ class Values
if (!array_key_exists($varName, $inputs)) {
$defaultValue = $definitionNode->defaultValue;
if ($defaultValue) {
$coercedValues[$varName] = Utils\AST::valueFromAST($defaultValue, $varType);
$coercedValues[$varName] = AST::valueFromAST($defaultValue, $varType);
}
if ($varType instanceof NonNull) {
throw new Error(
@ -148,7 +150,7 @@ class Values
}
} else {
$valueNode = $argumentNode->value;
$coercedValue = Utils\AST::valueFromAST($valueNode, $argType, $variableValues);
$coercedValue = AST::valueFromAST($valueNode, $argType, $variableValues);
if ($coercedValue === $undefined) {
$errors = DocumentValidator::isValidLiteralValue($argType, $valueNode);
$message = !empty($errors) ? ("\n" . implode("\n", $errors)) : '';
@ -191,7 +193,7 @@ class Values
}
/**
* @deprecated as of 8.0 (Moved to Utils\AST::valueFromAST)
* @deprecated as of 8.0 (Moved to \GraphQL\Utils\AST::valueFromAST)
*
* @param $valueNode
* @param InputType $type
@ -200,7 +202,7 @@ class Values
*/
public static function valueFromAST($valueNode, InputType $type, $variables = null)
{
return Utils\AST::valueFromAST($valueNode, $type, $variables);
return AST::valueFromAST($valueNode, $type, $variables);
}
/**

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Language\AST;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
abstract class Node
{

View File

@ -2,7 +2,7 @@
namespace GraphQL\Language;
use GraphQL\Error\SyntaxError;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* A Lexer is a stateful stream generator in that every time

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Language;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class Source
{

View File

@ -10,6 +10,8 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Introspection;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
/**
* Schema Definition
@ -235,7 +237,7 @@ class Schema
}
$typeMap = [];
foreach ($initialTypes as $type) {
$typeMap = Utils\TypeInfo::extractTypes($type, $typeMap);
$typeMap = TypeInfo::extractTypes($type, $typeMap);
}
return $typeMap + Type::getInternalTypes();
}

View File

@ -4,7 +4,7 @@ namespace GraphQL\Schema;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class Config

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Schema;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class Descriptor implements \JsonSerializable
{

View File

@ -13,6 +13,7 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Resolution;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Utils\Utils;
class Server
{

View File

@ -3,7 +3,7 @@ namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class Config

View File

@ -2,7 +2,8 @@
namespace GraphQL\Type\Definition;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Utils;
use GraphQL\Utils\MixedStore;
use GraphQL\Utils\Utils;
/**
* Class EnumType
@ -16,7 +17,7 @@ class EnumType extends Type implements InputType, OutputType, LeafType
private $values;
/**
* @var Utils\MixedStore<mixed, EnumValueDefinition>
* @var MixedStore<mixed, EnumValueDefinition>
*/
private $valueLookup;
@ -138,12 +139,12 @@ class EnumType extends Type implements InputType, OutputType, LeafType
}
/**
* @return Utils\MixedStore<mixed, EnumValueDefinition>
* @return MixedStore<mixed, EnumValueDefinition>
*/
private function getValueLookup()
{
if (null === $this->valueLookup) {
$this->valueLookup = new Utils\MixedStore();
$this->valueLookup = new MixedStore();
foreach ($this->getValues() as $valueName => $value) {
$this->valueLookup->offsetSet($value->value, $value);

View File

@ -1,6 +1,6 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class EnumValueDefinition

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class FieldDefinition

View File

@ -4,7 +4,7 @@ namespace GraphQL\Type\Definition;
use GraphQL\Error\UserError;
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class FloatType

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class InputObjectType

View File

@ -4,7 +4,7 @@ namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\UserError;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class IntType

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class InterfaceType

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class ListOfType

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class NonNull

View File

@ -2,7 +2,7 @@
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\UserError;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**

View File

@ -9,7 +9,7 @@ use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Schema;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class ResolveInfo

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Scalar Type Definition

View File

@ -2,7 +2,7 @@
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/*
export type GraphQLType =

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Type\Definition;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class UnionType

View File

@ -9,7 +9,8 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
/**
* EXPERIMENTAL!
@ -38,7 +39,7 @@ class EagerResolution implements Resolution
{
$typeMap = [];
foreach ($initialTypes as $type) {
$typeMap = Utils\TypeInfo::extractTypes($type, $typeMap);
$typeMap = TypeInfo::extractTypes($type, $typeMap);
}
$this->typeMap = $typeMap + Type::getInternalTypes();

View File

@ -20,7 +20,7 @@ use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Utils\AST;
class TypeKind {

View File

@ -5,7 +5,7 @@ use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\AbstractType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* EXPERIMENTAL!

View File

@ -1,372 +1,11 @@
<?php
namespace GraphQL;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Utils\SchemaUtils;
use \Traversable, \InvalidArgumentException;
trigger_error(
'GraphQL\Utils was moved to GraphQL\Utils\Utils and will be deleted on next release',
E_USER_DEPRECATED
);
class Utils
class Utils extends \GraphQL\Utils\Utils
{
public static function undefined()
{
static $undefined;
return $undefined ?: $undefined = new \stdClass();
}
/**
* @param object $obj
* @param array $vars
* @param array $requiredKeys
*
* @return array
*/
public static function assign($obj, array $vars, array $requiredKeys = [])
{
foreach ($requiredKeys as $key) {
if (!isset($vars[$key])) {
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
}
}
foreach ($vars as $key => $value) {
if (!property_exists($obj, $key)) {
$cls = get_class($obj);
Warning::warn(
"Trying to set non-existing property '$key' on class '$cls'",
Warning::ASSIGN_WARNING
);
}
$obj->{$key} = $value;
}
return $obj;
}
/**
* @param array|Traversable $traversable
* @param callable $predicate
* @return null
*/
public static function find($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $value) {
if ($predicate($value, $key)) {
return $value;
}
}
return null;
}
/**
* @param $traversable
* @param callable $predicate
* @return array
* @throws \Exception
*/
public static function filter($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$result = [];
$assoc = false;
foreach ($traversable as $key => $value) {
if (!$assoc && !is_int($key)) {
$assoc = true;
}
if ($predicate($value, $key)) {
$result[$key] = $value;
}
}
return $assoc ? $result : array_values($result);
}
/**
* @param array|\Traversable $traversable
* @param callable $fn function($value, $key) => $newValue
* @return array
* @throws \Exception
*/
public static function map($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$map[$key] = $fn($value, $key);
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
* @return array
* @throws \Exception
*/
public static function mapKeyValue($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
list($newKey, $newValue) = $fn($value, $key);
$map[$newKey] = $newValue;
}
return $map;
}
/**
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey
* @return array
* @throws \Exception
*/
public static function keyMap($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$newKey = $keyFn($value, $key);
if (is_scalar($newKey)) {
$map[$newKey] = $value;
}
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
*/
public static function each($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $item) {
$fn($item, $key);
}
}
/**
* Splits original traversable to several arrays with keys equal to $keyFn return
*
* E.g. Utils::groupBy([1, 2, 3, 4, 5], function($value) {return $value % 3}) will output:
* [
* 1 => [1, 4],
* 2 => [2, 5],
* 0 => [3],
* ]
*
* $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys
*
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey(s)
* @return array
*/
public static function groupBy($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$grouped = [];
foreach ($traversable as $key => $value) {
$newKeys = (array) $keyFn($value, $key);
foreach ($newKeys as $key) {
$grouped[$key][] = $value;
}
}
return $grouped;
}
public static function keyValMap($traversable, callable $keyFn, callable $valFn)
{
return array_reduce($traversable, function ($map, $item) use ($keyFn, $valFn) {
$map[$keyFn($item)] = $valFn($item);
return $map;
}, []);
}
/**
* @param $traversable
* @param callable $predicate
* @return bool
*/
public static function every($traversable, callable $predicate)
{
foreach ($traversable as $key => $value) {
if (!$predicate($value, $key)) {
return false;
}
}
return true;
}
/**
* @param $test
* @param string $message
* @param mixed $sprintfParam1
* @param mixed $sprintfParam2 ...
* @throws \Exception
*/
public static function invariant($test, $message = '')
{
if (!$test) {
if (func_num_args() > 2) {
$args = func_get_args();
array_shift($args);
$message = call_user_func_array('sprintf', $args);
}
throw new InvariantViolation($message);
}
}
/**
* @param $var
* @return string
*/
public static function getVariableType($var)
{
if ($var instanceof Type) {
// FIXME: Replace with schema printer call
if ($var instanceof WrappingType) {
$var = $var->getWrappedType(true);
}
return $var->name;
}
return is_object($var) ? get_class($var) : gettype($var);
}
/**
* @param $var
* @return string
*/
public static function printSafe($var)
{
if ($var instanceof Type) {
return $var->toString();
}
if (is_object($var)) {
return 'instance of ' . get_class($var);
}
if ('' === $var) {
return '(empty string)';
}
if (is_string($var)) {
return "\"$var\"";
}
if (is_scalar($var)) {
return (string) $var;
}
if (null === $var) {
return 'null';
}
return gettype($var);
}
/**
* UTF-8 compatible chr()
*
* @param string $ord
* @param string $encoding
* @return string
*/
public static function chr($ord, $encoding = 'UTF-8')
{
if ($ord <= 255) {
return chr($ord);
}
if ($encoding === 'UCS-4BE') {
return pack("N", $ord);
} else {
return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
}
}
/**
* UTF-8 compatible ord()
*
* @param string $char
* @param string $encoding
* @return mixed
*/
public static function ord($char, $encoding = 'UTF-8')
{
if (!$char && '0' !== $char) {
return 0;
}
if (!isset($char[1])) {
return ord($char);
}
if ($encoding !== 'UCS-4BE') {
$char = mb_convert_encoding($char, 'UCS-4BE', $encoding);
}
list(, $ord) = unpack('N', $char);
return $ord;
}
/**
* Returns UTF-8 char code at given $positing of the $string
*
* @param $string
* @param $position
* @return mixed
*/
public static function charCodeAt($string, $position)
{
$char = mb_substr($string, $position, 1, 'UTF-8');
return self::ord($char);
}
/**
* @param $code
* @return string
*/
public static function printCharCode($code)
{
if (null === $code) {
return '<EOF>';
}
return $code < 0x007F
// Trust JSON for ASCII.
? json_encode(Utils::chr($code))
// Otherwise print the escaped form.
: '"\\u' . dechex($code) . '"';
}
/**
* @param $name
* @param bool $isIntrospection
* @throws Error
*/
public static function assertValidName($name, $isIntrospection = false)
{
$regex = '/^[_a-zA-Z][_a-zA-Z0-9]*$/';
if (!$name || !is_string($name)) {
throw new InvariantViolation(
"Must be named. Unexpected name: " . self::printSafe($name)
);
}
if (!$isIntrospection && isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
Warning::warnOnce(
'Name "'.$name.'" must not begin with "__", which is reserved by ' .
'GraphQL introspection. In a future release of graphql this will ' .
'become an exception',
Warning::NAME_WARNING
);
}
if (!preg_match($regex, $name)) {
throw new InvariantViolation(
'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "'.$name.'" does not.'
);
}
}
}

View File

@ -22,7 +22,7 @@ use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\LeafType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class AST

View File

@ -31,7 +31,6 @@ use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Introspection;
use GraphQL\Utils;
/**
* Class BuildSchema
@ -394,7 +393,7 @@ class BuildSchema
'description' => $this->getDescription($value)
];
if (isset($value->defaultValue)) {
$config['defaultValue'] = Utils\AST::valueFromAST($value->defaultValue, $type);
$config['defaultValue'] = AST::valueFromAST($value->defaultValue, $type);
}
return $config;
}

View File

@ -1,6 +1,6 @@
<?php
namespace GraphQL\Utils;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Similar to PHP array, but allows any type of data to act as key (including arrays, objects, scalars)

View File

@ -12,7 +12,7 @@ use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class SchemaPrinter

View File

@ -24,7 +24,7 @@ use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\WrappingType;
use GraphQL\Type\Introspection;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
/**
* Class TypeInfo

371
src/Utils/Utils.php Normal file
View File

@ -0,0 +1,371 @@
<?php
namespace GraphQL\Utils;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType;
use \Traversable, \InvalidArgumentException;
class Utils
{
public static function undefined()
{
static $undefined;
return $undefined ?: $undefined = new \stdClass();
}
/**
* @param object $obj
* @param array $vars
* @param array $requiredKeys
*
* @return array
*/
public static function assign($obj, array $vars, array $requiredKeys = [])
{
foreach ($requiredKeys as $key) {
if (!isset($vars[$key])) {
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
}
}
foreach ($vars as $key => $value) {
if (!property_exists($obj, $key)) {
$cls = get_class($obj);
Warning::warn(
"Trying to set non-existing property '$key' on class '$cls'",
Warning::ASSIGN_WARNING
);
}
$obj->{$key} = $value;
}
return $obj;
}
/**
* @param array|Traversable $traversable
* @param callable $predicate
* @return null
*/
public static function find($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $value) {
if ($predicate($value, $key)) {
return $value;
}
}
return null;
}
/**
* @param $traversable
* @param callable $predicate
* @return array
* @throws \Exception
*/
public static function filter($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$result = [];
$assoc = false;
foreach ($traversable as $key => $value) {
if (!$assoc && !is_int($key)) {
$assoc = true;
}
if ($predicate($value, $key)) {
$result[$key] = $value;
}
}
return $assoc ? $result : array_values($result);
}
/**
* @param array|\Traversable $traversable
* @param callable $fn function($value, $key) => $newValue
* @return array
* @throws \Exception
*/
public static function map($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$map[$key] = $fn($value, $key);
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
* @return array
* @throws \Exception
*/
public static function mapKeyValue($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
list($newKey, $newValue) = $fn($value, $key);
$map[$newKey] = $newValue;
}
return $map;
}
/**
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey
* @return array
* @throws \Exception
*/
public static function keyMap($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$newKey = $keyFn($value, $key);
if (is_scalar($newKey)) {
$map[$newKey] = $value;
}
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
*/
public static function each($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $item) {
$fn($item, $key);
}
}
/**
* Splits original traversable to several arrays with keys equal to $keyFn return
*
* E.g. Utils::groupBy([1, 2, 3, 4, 5], function($value) {return $value % 3}) will output:
* [
* 1 => [1, 4],
* 2 => [2, 5],
* 0 => [3],
* ]
*
* $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys
*
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey(s)
* @return array
*/
public static function groupBy($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$grouped = [];
foreach ($traversable as $key => $value) {
$newKeys = (array) $keyFn($value, $key);
foreach ($newKeys as $key) {
$grouped[$key][] = $value;
}
}
return $grouped;
}
public static function keyValMap($traversable, callable $keyFn, callable $valFn)
{
return array_reduce($traversable, function ($map, $item) use ($keyFn, $valFn) {
$map[$keyFn($item)] = $valFn($item);
return $map;
}, []);
}
/**
* @param $traversable
* @param callable $predicate
* @return bool
*/
public static function every($traversable, callable $predicate)
{
foreach ($traversable as $key => $value) {
if (!$predicate($value, $key)) {
return false;
}
}
return true;
}
/**
* @param $test
* @param string $message
* @param mixed $sprintfParam1
* @param mixed $sprintfParam2 ...
* @throws \Exception
*/
public static function invariant($test, $message = '')
{
if (!$test) {
if (func_num_args() > 2) {
$args = func_get_args();
array_shift($args);
$message = call_user_func_array('sprintf', $args);
}
throw new InvariantViolation($message);
}
}
/**
* @param $var
* @return string
*/
public static function getVariableType($var)
{
if ($var instanceof Type) {
// FIXME: Replace with schema printer call
if ($var instanceof WrappingType) {
$var = $var->getWrappedType(true);
}
return $var->name;
}
return is_object($var) ? get_class($var) : gettype($var);
}
/**
* @param $var
* @return string
*/
public static function printSafe($var)
{
if ($var instanceof Type) {
return $var->toString();
}
if (is_object($var)) {
return 'instance of ' . get_class($var);
}
if ('' === $var) {
return '(empty string)';
}
if (is_string($var)) {
return "\"$var\"";
}
if (is_scalar($var)) {
return (string) $var;
}
if (null === $var) {
return 'null';
}
return gettype($var);
}
/**
* UTF-8 compatible chr()
*
* @param string $ord
* @param string $encoding
* @return string
*/
public static function chr($ord, $encoding = 'UTF-8')
{
if ($ord <= 255) {
return chr($ord);
}
if ($encoding === 'UCS-4BE') {
return pack("N", $ord);
} else {
return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
}
}
/**
* UTF-8 compatible ord()
*
* @param string $char
* @param string $encoding
* @return mixed
*/
public static function ord($char, $encoding = 'UTF-8')
{
if (!$char && '0' !== $char) {
return 0;
}
if (!isset($char[1])) {
return ord($char);
}
if ($encoding !== 'UCS-4BE') {
$char = mb_convert_encoding($char, 'UCS-4BE', $encoding);
}
list(, $ord) = unpack('N', $char);
return $ord;
}
/**
* Returns UTF-8 char code at given $positing of the $string
*
* @param $string
* @param $position
* @return mixed
*/
public static function charCodeAt($string, $position)
{
$char = mb_substr($string, $position, 1, 'UTF-8');
return self::ord($char);
}
/**
* @param $code
* @return string
*/
public static function printCharCode($code)
{
if (null === $code) {
return '<EOF>';
}
return $code < 0x007F
// Trust JSON for ASCII.
? json_encode(Utils::chr($code))
// Otherwise print the escaped form.
: '"\\u' . dechex($code) . '"';
}
/**
* @param $name
* @param bool $isIntrospection
* @throws Error
*/
public static function assertValidName($name, $isIntrospection = false)
{
$regex = '/^[_a-zA-Z][_a-zA-Z0-9]*$/';
if (!$name || !is_string($name)) {
throw new InvariantViolation(
"Must be named. Unexpected name: " . self::printSafe($name)
);
}
if (!$isIntrospection && isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
Warning::warnOnce(
'Name "'.$name.'" must not begin with "__", which is reserved by ' .
'GraphQL introspection. In a future release of graphql this will ' .
'become an exception',
Warning::NAME_WARNING
);
}
if (!preg_match($regex, $name)) {
throw new InvariantViolation(
'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "'.$name.'" does not.'
);
}
}
}

View File

@ -15,7 +15,7 @@ use GraphQL\Type\Definition\LeafType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\Rules\ArgumentsOfCorrectType;
use GraphQL\Validator\Rules\DefaultValuesOfCorrectType;

View File

@ -6,7 +6,7 @@ use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Schema;
use GraphQL\Type\Definition\AbstractType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;
class FieldsOnCorrectType

View File

@ -4,7 +4,7 @@ namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;
class KnownArgumentNames

View File

@ -6,7 +6,7 @@ use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;
/**

View File

@ -15,7 +15,7 @@ use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Visitor;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;
class NoFragmentCycles

View File

@ -18,7 +18,7 @@ use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\OutputType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Utils\PairSet;
use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\ValidationContext;

View File

@ -9,7 +9,7 @@ use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Visitor;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Validator\ValidationContext;
class ProvidedNonNullArguments

View File

@ -9,7 +9,7 @@ use GraphQL\Language\AST\VariableDefinitionNode;
use GraphQL\Language\Printer;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\TypeInfo;
use GraphQL\Validator\ValidationContext;
class VariablesAreInputTypes
@ -23,7 +23,7 @@ class VariablesAreInputTypes
{
return [
NodeKind::VARIABLE_DEFINITION => function(VariableDefinitionNode $node) use ($context) {
$type = Utils\TypeInfo::typeFromAST($context->getSchema(), $node->type);
$type = TypeInfo::typeFromAST($context->getSchema(), $node->type);
// If the variable type is not an input type, return an error.
if ($type && !Type::isInputType($type)) {

View File

@ -9,7 +9,7 @@ use GraphQL\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class DeferredFieldsTest extends \PHPUnit_Framework_TestCase
{

View File

@ -5,7 +5,7 @@ use GraphQL\Language\Lexer;
use GraphQL\Language\Source;
use GraphQL\Language\Token;
use GraphQL\Error\SyntaxError;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class LexerTest extends \PHPUnit_Framework_TestCase
{

View File

@ -13,7 +13,7 @@ use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
use GraphQL\Error\SyntaxError;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class ParserTest extends \PHPUnit_Framework_TestCase
{

View File

@ -8,7 +8,7 @@ use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class ConfigTest extends \PHPUnit_Framework_TestCase
{

View File

@ -13,7 +13,7 @@ use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class DefinitionTest extends \PHPUnit_Framework_TestCase
{

View File

@ -2,7 +2,7 @@
namespace GraphQL\Tests\Utils;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Utils\MixedStore;
class MixedStoreTest extends \PHPUnit_Framework_TestCase

View File

@ -6,7 +6,7 @@ use GraphQL\Language\Parser;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
use GraphQL\Utils\AST;
class ValueFromAstTest extends \PHPUnit_Framework_TestCase

View File

@ -1,7 +1,7 @@
<?php
namespace GraphQL\Tests;
use GraphQL\Utils;
use GraphQL\Utils\Utils;
class UtilsTest extends \PHPUnit_Framework_TestCase
{