當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Type::coerceLiteral方法代碼示例

本文整理匯總了PHP中GraphQL\Type\Definition\Type::coerceLiteral方法的典型用法代碼示例。如果您正苦於以下問題:PHP Type::coerceLiteral方法的具體用法?PHP Type::coerceLiteral怎麽用?PHP Type::coerceLiteral使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在GraphQL\Type\Definition\Type的用法示例。


在下文中一共展示了Type::coerceLiteral方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: coerceValueAST

 /**
  * Given a type and a value AST node known to match this type, build a
  * runtime value.
  */
 private static function coerceValueAST(Type $type, $valueAST, $variables)
 {
     if ($type instanceof NonNull) {
         // Note: we're not checking that the result of coerceValueAST is non-null.
         // We're assuming that this query has been validated and the value used
         // here is of the correct type.
         return self::coerceValueAST($type->getWrappedType(), $valueAST, $variables);
     }
     if (!$valueAST) {
         return null;
     }
     if ($valueAST->kind === Node::VARIABLE) {
         $variableName = $valueAST->name->value;
         if (!isset($variables, $variables[$variableName])) {
             return null;
         }
         // Note: we're not doing any checking that this variable is correct. We're
         // assuming that this query has been validated and the variable usage here
         // is of the correct type.
         return $variables[$variableName];
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         if ($valueAST->kind === Node::ARR) {
             $tmp = [];
             foreach ($valueAST->values as $itemAST) {
                 $tmp[] = self::coerceValueAST($itemType, $itemAST, $variables);
             }
             return $tmp;
         } else {
             return [self::coerceValueAST($itemType, $valueAST, $variables)];
         }
     }
     if ($type instanceof InputObjectType) {
         $fields = $type->getFields();
         if ($valueAST->kind !== Node::OBJECT) {
             return null;
         }
         $fieldASTs = Utils::keyMap($valueAST->fields, function ($field) {
             return $field->name->value;
         });
         $obj = [];
         foreach ($fields as $fieldName => $field) {
             $fieldAST = $fieldASTs[$fieldName];
             $fieldValue = self::coerceValueAST($field->getType(), $fieldAST ? $fieldAST->value : null, $variables);
             $obj[$fieldName] = $fieldValue === null ? $field->defaultValue : $fieldValue;
         }
         return $obj;
     }
     if ($type instanceof ScalarType || $type instanceof EnumType) {
         $coerced = $type->coerceLiteral($valueAST);
         if (null !== $coerced) {
             return $coerced;
         }
     }
     return null;
 }
開發者ID:rtuin,項目名稱:graphql-php,代碼行數:61,代碼來源:Values.php


注:本文中的GraphQL\Type\Definition\Type::coerceLiteral方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。