Automattic\WooCommerce\Vendor\GraphQL\Executor

Values::getArgumentValuesForMappublic staticWC 1.0

Method of the class: Values{}

No Hooks.

Returns

Array. mixed>

Usage

$result = Values::getArgumentValuesForMap( $def, $argumentValueMap, ?array $variableValues, ?Node $referenceNode, ?Schema $schema ): array;
$def(FieldDefinition|Directive) (required)
.
$argumentValueMap(array) (required)
.
?array $variableValues
.
Default: null
?Node $referenceNode
.
Default: null
?Schema $schema
.
Default: null

Values::getArgumentValuesForMap() code WC 10.9.1

public static function getArgumentValuesForMap($def, array $argumentValueMap, ?array $variableValues = null, ?Node $referenceNode = null, ?Schema $schema = null): array
{
    /** @var array<string, mixed> $coercedValues */
    $coercedValues = [];

    foreach ($def->args as $argumentDefinition) {
        $name = $argumentDefinition->name;
        $argType = $argumentDefinition->getType();
        $argumentValueNode = $argumentValueMap[$name] ?? null;

        if ($argumentValueNode instanceof VariableNode) {
            $variableName = $argumentValueNode->name->value;
            $hasValue = $variableValues !== null && array_key_exists($variableName, $variableValues);
            $isNull = $hasValue && $variableValues[$variableName] === null;
        } else {
            $hasValue = $argumentValueNode !== null;
            $isNull = $argumentValueNode instanceof NullValueNode;
        }

        if (! $hasValue && $argumentDefinition->defaultValueExists()) {
            // If no argument was provided where the definition has a default value,
            // use the default value.
            $coercedValues[$name] = $argumentDefinition->defaultValue;
        } elseif ((! $hasValue || $isNull) && ($argType instanceof NonNull)) {
            // If no argument or a null value was provided to an argument with a
            // non-null type (required), produce a field error.
            $safeArgType = Utils::printSafe($argType);

            if ($isNull) {
                throw new Error("Argument \"{$name}\" of non-null type \"{$safeArgType}\" must not be null.", $referenceNode);
            }

            if ($argumentValueNode instanceof VariableNode) {
                throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was provided the variable \"\${$argumentValueNode->name->value}\" which was not provided a runtime value.", [$argumentValueNode]);
            }

            throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was not provided.", $referenceNode);
        } elseif ($hasValue) {
            assert($argumentValueNode instanceof Node);

            if ($argumentValueNode instanceof NullValueNode) {
                // If the explicit value `null` was provided, an entry in the coerced
                // values must exist as the value `null`.
                $coercedValues[$name] = null;
            } elseif ($argumentValueNode instanceof VariableNode) {
                $variableName = $argumentValueNode->name->value;
                // Note: This does no further checking that this variable is correct.
                // This assumes that this query has been validated and the variable
                // usage here is of the correct type.
                $coercedValues[$name] = $variableValues[$variableName] ?? null;
            } else {
                $coercedValue = AST::valueFromAST($argumentValueNode, $argType, $variableValues, $schema);
                if (Utils::undefined() === $coercedValue) {
                    // Note: ValuesOfCorrectType validation should catch this before
                    // execution. This is a runtime check to ensure execution does not
                    // continue with an invalid argument value.
                    $invalidValue = Printer::doPrint($argumentValueNode);
                    throw new Error("Argument \"{$name}\" has invalid value {$invalidValue}.", [$argumentValueNode]);
                }

                $coercedValues[$name] = $coercedValue;
            }
        }
    }

    return $coercedValues;
}