packages/sql-catalog/src/Core/Evaluation/ArrayEntry.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlCatalog\Core\Evaluation;
6
7/**
8 * One element of a statically reconstructed array.
9 *
10 * @visibility root
11 */
12final class ArrayEntry
13{
14    /**
15     * @param Domain|null $key The key, or null when the element takes the next integer key
16     * @param Domain $value The element value
17     */
18    public function __construct(
19        public readonly ?Domain $key,
20        public readonly Domain $value,
21    ) {
22    }
23
24    /**
25     * The key when it resolved to a single scalar, otherwise null.
26     */
27    public function scalarKey(): string|int|null
28    {
29        $literal = $this->key?->soleLiteral();
30        if ($literal === null) {
31            return null;
32        }
33        if (is_int($literal->value)) {
34            return $literal->value;
35        }
36
37        return is_string($literal->value) ? $literal->value : null;
38    }
39
40    /**
41     * A canonical string used to deduplicate arrays.
42     */
43    public function signature(): string
44    {
45        return ($this->key?->signature() ?? '_') . '=>' . $this->value->signature();
46    }
47}
48