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


PHP Type::parseValue方法代碼示例

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


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

示例1: coerceValue

 /**
  * Given a type and any value, return a runtime value coerced to match the type.
  */
 private static function coerceValue(Type $type, $value)
 {
     if ($type instanceof NonNull) {
         // Note: we're not checking that the result of coerceValue is non-null.
         // We only call this function after calling isValidValue.
         return self::coerceValue($type->getWrappedType(), $value);
     }
     if (null === $value) {
         return null;
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         // TODO: support iterable input
         if (is_array($value)) {
             return array_map(function ($item) use($itemType) {
                 return Values::coerceValue($itemType, $item);
             }, $value);
         } else {
             return [self::coerceValue($itemType, $value)];
         }
     }
     if ($type instanceof InputObjectType) {
         $fields = $type->getFields();
         $obj = [];
         foreach ($fields as $fieldName => $field) {
             $fieldValue = self::coerceValue($field->getType(), isset($value[$fieldName]) ? $value[$fieldName] : null);
             if (null === $fieldValue) {
                 $fieldValue = $field->defaultValue;
             }
             if (null !== $fieldValue) {
                 $obj[$fieldName] = $fieldValue;
             }
         }
         return $obj;
     }
     Utils::invariant($type instanceof ScalarType || $type instanceof EnumType, 'Must be input type');
     return $type->parseValue($value);
 }
開發者ID:mrsinguyen,項目名稱:graphql-php,代碼行數:41,代碼來源:Values.php

示例2: coerceValue

 /**
  * Given a type and any value, return a runtime value coerced to match the type.
  */
 private static function coerceValue(Type $type, $value)
 {
     $undefined = Utils::undefined();
     if ($value === $undefined) {
         return $undefined;
     }
     if ($type instanceof NonNull) {
         if ($value === null) {
             // Intentionally return no value.
             return $undefined;
         }
         return self::coerceValue($type->getWrappedType(), $value);
     }
     if (null === $value) {
         return null;
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         if (is_array($value) || $value instanceof \Traversable) {
             $coercedValues = [];
             foreach ($value as $item) {
                 $itemValue = self::coerceValue($itemType, $item);
                 if ($undefined === $itemValue) {
                     // Intentionally return no value.
                     return $undefined;
                 }
                 $coercedValues[] = $itemValue;
             }
             return $coercedValues;
         } else {
             $coercedValue = self::coerceValue($itemType, $value);
             if ($coercedValue === $undefined) {
                 // Intentionally return no value.
                 return $undefined;
             }
             return [$coercedValue];
         }
     }
     if ($type instanceof InputObjectType) {
         $coercedObj = [];
         $fields = $type->getFields();
         foreach ($fields as $fieldName => $field) {
             if (!array_key_exists($fieldName, $value)) {
                 if ($field->defaultValueExists()) {
                     $coercedObj[$fieldName] = $field->defaultValue;
                 } else {
                     if ($field->getType() instanceof NonNull) {
                         // Intentionally return no value.
                         return $undefined;
                     }
                 }
                 continue;
             }
             $fieldValue = self::coerceValue($field->getType(), $value[$fieldName]);
             if ($fieldValue === $undefined) {
                 // Intentionally return no value.
                 return $undefined;
             }
             $coercedObj[$fieldName] = $fieldValue;
         }
         return $coercedObj;
     }
     if ($type instanceof LeafType) {
         $parsed = $type->parseValue($value);
         if (null === $parsed) {
             // null or invalid values represent a failure to parse correctly,
             // in which case no value is returned.
             return $undefined;
         }
         return $parsed;
     }
     throw new InvariantViolation('Must be input type');
 }
開發者ID:webonyx,項目名稱:graphql-php,代碼行數:76,代碼來源:Values.php

示例3: isValidValue

 /**
  * Check if value is valid using GraphQL Type
  *
  * @param array      $value
  * @param Type $type
  *
  * @return boolean
  */
 private function isValidValue($value, Type $type)
 {
     if ($type instanceof NonNull) {
         if (null === $value) {
             return false;
         }
         return $this->isValidValue($value, $type->getWrappedType());
     }
     if ($value === null) {
         return true;
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         if (is_array($value)) {
             foreach ($value as $item) {
                 if (!$this->isValidValue($item, $itemType)) {
                     return false;
                 }
             }
             return true;
         } else {
             return $this->isValidValue($value, $itemType);
         }
     }
     if ($type instanceof InputObjectType) {
         if (!is_array($value)) {
             return false;
         }
         $fields = $type->getFields();
         $fieldMap = [];
         foreach ($fields as $fieldName => $field) {
             if (!$this->isValidValue(isset($value[$fieldName]) ? $value[$fieldName] : null, $field->getType())) {
                 return false;
             }
             $fieldMap[$field->name] = $field;
         }
         $diff = array_diff_key($value, $fieldMap);
         if (!empty($diff)) {
             return false;
         }
         return true;
     }
     Utils::invariant($type instanceof ScalarType || $type instanceof EnumType, 'Must be input type');
     return null !== $type->parseValue($value);
 }
開發者ID:webtales,項目名稱:alambic,代碼行數:53,代碼來源:Alambic.php

示例4: coerceValue

 /**
  * Given a type and any value, return a runtime value coerced to match the type.
  */
 private static function coerceValue(Type $type, $value)
 {
     if ($type instanceof NonNull) {
         // Note: we're not checking that the result of coerceValue is non-null.
         // We only call this function after calling isValidPHPValue.
         return self::coerceValue($type->getWrappedType(), $value);
     }
     if (null === $value) {
         return null;
     }
     if ($type instanceof ListOfType) {
         $itemType = $type->getWrappedType();
         if (is_array($value) || $value instanceof \Traversable) {
             return Utils::map($value, function ($item) use($itemType) {
                 return Values::coerceValue($itemType, $item);
             });
         } else {
             return [self::coerceValue($itemType, $value)];
         }
     }
     if ($type instanceof InputObjectType) {
         $fields = $type->getFields();
         $obj = [];
         foreach ($fields as $fieldName => $field) {
             $fieldValue = self::coerceValue($field->getType(), isset($value[$fieldName]) ? $value[$fieldName] : null);
             if (null === $fieldValue) {
                 $fieldValue = $field->defaultValue;
             }
             if (null !== $fieldValue) {
                 $obj[$fieldName] = $fieldValue;
             }
         }
         return $obj;
     }
     if ($type instanceof LeafType) {
         return $type->parseValue($value);
     }
     throw new InvariantViolation('Must be input type');
 }
開發者ID:aeshion,項目名稱:ZeroPHP,代碼行數:42,代碼來源:Values.php


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