Automattic\WooCommerce\Vendor\GraphQL\Utils

AST::fromArraypublic staticWC 1.0

Convert representation of AST as an associative array to instance of Automattic\WooCommerce\Vendor\GraphQL\Language\AST\Node.

For example:

AST::fromArray([
	'kind' => 'ListValue',
	'values' => [
		['kind' => 'StringValue', 'value' => 'my str'],
		['kind' => 'StringValue', 'value' => 'my other str']
	],
	'loc' => ['start' => 21, 'end' => 25]
]);

Will produce instance of ListValueNode where values prop is a lazily-evaluated NodeList returning instances of StringValueNode on access.

This is a reverse operation for AST::toArray($node)

Method of the class: AST{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = AST::fromArray( $node ): Node;
$node(array) (required)
.

AST::fromArray() code WC 10.9.1

public static function fromArray(array $node): Node
{
    $kind = $node['kind'] ?? null;
    if ($kind === null) {
        $safeNode = Utils::printSafeJson($node);
        throw new InvariantViolation("Node is missing kind: {$safeNode}");
    }

    $class = NodeKind::CLASS_MAP[$kind] ?? null;
    if ($class === null) {
        $safeNode = Utils::printSafeJson($node);
        throw new InvariantViolation("Node has unexpected kind: {$safeNode}");
    }

    $instance = new $class([]);

    if (isset($node['loc']['start'], $node['loc']['end'])) {
        $instance->loc = Location::create($node['loc']['start'], $node['loc']['end']);
    }

    foreach ($node as $key => $value) {
        if ($key === 'loc' || $key === 'kind') {
            continue;
        }

        if (is_array($value)) {
            $value = isset($value[0]) || $value === []
                ? new NodeList($value)
                : self::fromArray($value);
        }

        $instance->{$key} = $value;
    }

    return $instance;
}