本文整理汇总了PHP中GraphQL\Utils类的典型用法代码示例。如果您正苦于以下问题:PHP Utils类的具体用法?PHP Utils怎么用?PHP Utils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Utils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
public function __invoke(ValidationContext $context)
{
return [Node::FIELD => function (Field $fieldAST) use($context) {
$fieldDef = $context->getFieldDef();
if (!$fieldDef) {
return Visitor::skipNode();
}
$errors = [];
$argASTs = $fieldAST->arguments ?: [];
$argASTMap = Utils::keyMap($argASTs, function (Argument $arg) {
return $arg->name->value;
});
foreach ($fieldDef->args as $argDef) {
$argAST = isset($argASTMap[$argDef->name]) ? $argASTMap[$argDef->name] : null;
if (!$argAST && $argDef->getType() instanceof NonNull) {
$errors[] = new Error(Messages::missingArgMessage($fieldAST->name->value, $argDef->name, $argDef->getType()), [$fieldAST]);
}
}
$argDefMap = Utils::keyMap($fieldDef->args, function ($def) {
return $def->name;
});
foreach ($argASTs as $argAST) {
$argDef = $argDefMap[$argAST->name->value];
if ($argDef && !DocumentValidator::isValidLiteralValue($argAST->value, $argDef->getType())) {
$errors[] = new Error(Messages::badValueMessage($argAST->name->value, $argDef->getType(), Printer::doPrint($argAST->value)), [$argAST->value]);
}
}
return !empty($errors) ? $errors : null;
}];
}
示例2: parseValue
public function parseValue($value)
{
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new \Exception('Cannot represent value as email: ' . Utils::printSafe($value));
}
return $value;
}
示例3: doTypesOverlap
private function doTypesOverlap($t1, $t2)
{
if ($t1 === $t2) {
return true;
}
if ($t1 instanceof ObjectType) {
if ($t2 instanceof ObjectType) {
return false;
}
return in_array($t1, $t2->getPossibleTypes());
}
if ($t1 instanceof InterfaceType || $t1 instanceof UnionType) {
if ($t2 instanceof ObjectType) {
return in_array($t2, $t1->getPossibleTypes());
}
$t1TypeNames = Utils::keyMap($t1->getPossibleTypes(), function ($type) {
return $type->name;
});
foreach ($t2->getPossibleTypes() as $type) {
if (!empty($t1TypeNames[$type->name])) {
return true;
}
}
}
return false;
}
示例4: parseValue
public function parseValue($value)
{
if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
throw new \Exception('Cannot represent value as URL:' . Utils::printSafe($value));
}
return $value;
}
示例5: __construct
/**
* ScalarType constructor.
*/
public function __construct()
{
if (!isset($this->name)) {
$this->name = $this->tryInferName();
}
Utils::invariant($this->name, 'Type must be named.');
}
示例6: getField
/**
* @param $name
* @return FieldDefinition
* @throws \Exception
*/
public function getField($name)
{
if (null === $this->fields) {
$this->getFields();
}
Utils::invariant(isset($this->fields[$name]), 'Field "%s" is not defined for type "%s"', $name, $this->name);
return $this->fields[$name];
}
示例7: parseValue
/**
* Parses an externally provided value (query variable) to use as an input
*
* @param mixed $value
* @return mixed
*/
public function parseValue($value)
{
if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
// quite naive, but after all this is example
throw new \UnexpectedValueException("Cannot represent value as URL: " . Utils::printSafe($value));
}
return $value;
}
示例8: getTypeASTName
private function getTypeASTName(Type $typeAST)
{
if ($typeAST->kind === Node::NAME) {
return $typeAST->value;
}
Utils::invariant($typeAST->type, 'Must be wrapping type');
return $this->getTypeASTName($typeAST->type);
}
示例9: coerceFloat
/**
* @param $value
* @return float|null
*/
private function coerceFloat($value)
{
if ($value === '') {
throw new InvariantViolation('Float cannot represent non numeric value: (empty string)');
}
if (is_numeric($value) || $value === true || $value === false) {
return (double) $value;
}
throw new InvariantViolation('Float cannot represent non numeric value: ' . Utils::printSafe($value));
}
示例10: withModifiers
private function withModifiers($types)
{
return array_merge(Utils::map($types, function ($type) {
return Type::listOf($type);
}), Utils::map($types, function ($type) {
return Type::nonNull($type);
}), Utils::map($types, function ($type) {
return Type::nonNull(Type::listOf($type));
}));
}
示例11: __construct
public function __construct($config)
{
Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'values' => Config::arrayOf(['name' => Config::STRING | Config::REQUIRED, 'value' => Config::ANY, 'deprecationReason' => Config::STRING, 'description' => Config::STRING], Config::KEY_AS_NAME), 'description' => Config::STRING]);
$this->name = $config['name'];
$this->description = isset($config['description']) ? $config['description'] : null;
$this->_values = [];
if (!empty($config['values'])) {
foreach ($config['values'] as $name => $value) {
$this->_values[] = Utils::assign(new EnumValueDefinition(), $value + ['name' => $name]);
}
}
}
示例12: getImplementationsIncludingField
/**
* Return implementations of `type` that include `fieldName` as a valid field.
*
* @param Schema $schema
* @param AbstractType $type
* @param $fieldName
* @return array
*/
static function getImplementationsIncludingField(Schema $schema, AbstractType $type, $fieldName)
{
$types = $schema->getPossibleTypes($type);
$types = Utils::filter($types, function ($t) use($fieldName) {
return isset($t->getFields()[$fieldName]);
});
$types = Utils::map($types, function ($t) {
return $t->name;
});
sort($types);
return $types;
}
示例13: coerceInt
/**
* @param $value
* @return int|null
*/
private function coerceInt($value)
{
if ($value === '') {
throw new InvariantViolation('Int cannot represent non 32-bit signed integer value: (empty string)');
}
if (false === $value || true === $value) {
return (int) $value;
}
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
return (int) $value;
}
throw new InvariantViolation('Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value));
}
示例14: __invoke
public function __invoke(ValidationContext $context)
{
$operationCount = 0;
return [NodeKind::DOCUMENT => function (DocumentNode $node) use(&$operationCount) {
$tmp = Utils::filter($node->definitions, function ($definition) {
return $definition->kind === NodeKind::OPERATION_DEFINITION;
});
$operationCount = count($tmp);
}, NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use(&$operationCount, $context) {
if (!$node->name && $operationCount > 1) {
$context->reportError(new Error(self::anonOperationNotAloneMessage(), [$node]));
}
}];
}
示例15: __construct
public function __construct($config)
{
Config::validate($config, ['name' => Config::STRING | Config::REQUIRED, 'types' => Config::arrayOf(Config::OBJECT_TYPE | Config::REQUIRED), 'resolveType' => Config::CALLBACK, 'description' => Config::STRING]);
Utils::invariant(!empty($config['types']), "");
/**
* Optionally provide a custom type resolver function. If one is not provided,
* the default implemenation will call `isTypeOf` on each implementing
* Object type.
*/
$this->name = $config['name'];
$this->description = isset($config['description']) ? $config['description'] : null;
$this->_types = $config['types'];
$this->_resolveType = isset($config['resolveType']) ? $config['resolveType'] : null;
}