GraphQL\Executor\Values::isValidPHPValue PHP Method

isValidPHPValue() private static method

Given a PHP value and a GraphQL type, determine if the value will be accepted for that type. This is primarily useful for validating the runtime values of query variables.
private static isValidPHPValue ( $value, GraphQL\Type\Definition\InputType $type ) : array
$value
$type GraphQL\Type\Definition\InputType
return array
    private static function isValidPHPValue($value, InputType $type)
    {
        // A value must be provided if the type is non-null.
        if ($type instanceof NonNull) {
            if (null === $value) {
                return ['Expected "' . Utils::printSafe($type) . '", found null.'];
            }
            return self::isValidPHPValue($value, $type->getWrappedType());
        }
        if (null === $value) {
            return [];
        }
        // Lists accept a non-list value as a list of one.
        if ($type instanceof ListOfType) {
            $itemType = $type->getWrappedType();
            if (is_array($value)) {
                $tmp = [];
                foreach ($value as $index => $item) {
                    $errors = self::isValidPHPValue($item, $itemType);
                    $tmp = array_merge($tmp, Utils::map($errors, function ($error) use($index) {
                        return "In element #{$index}: {$error}";
                    }));
                }
                return $tmp;
            }
            return self::isValidPHPValue($value, $itemType);
        }
        // Input objects check each defined field.
        if ($type instanceof InputObjectType) {
            if (!is_object($value) && !is_array($value)) {
                return ["Expected \"{$type->name}\", found not an object."];
            }
            $fields = $type->getFields();
            $errors = [];
            // Ensure every provided field is defined.
            $props = is_object($value) ? get_object_vars($value) : $value;
            foreach ($props as $providedField => $tmp) {
                if (!isset($fields[$providedField])) {
                    $errors[] = "In field \"{$providedField}\": Unknown field.";
                }
            }
            // Ensure every defined field is valid.
            foreach ($fields as $fieldName => $tmp) {
                $newErrors = self::isValidPHPValue(isset($value[$fieldName]) ? $value[$fieldName] : null, $fields[$fieldName]->getType());
                $errors = array_merge($errors, Utils::map($newErrors, function ($error) use($fieldName) {
                    return "In field \"{$fieldName}\": {$error}";
                }));
            }
            return $errors;
        }
        if ($type instanceof LeafType) {
            // Scalar/Enum input checks to ensure the type can parse the value to
            // a non-null value.
            $parseResult = $type->parseValue($value);
            if (null === $parseResult) {
                $v = json_encode($value);
                return ["Expected type \"{$type->name}\", found {$v}."];
            }
            return [];
        }
        throw new InvariantViolation('Must be input type');
    }