packages/sql-semantics-postgres/src/Rules/Table/Command/KeyTerms.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Table\Command;
6
7use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\PostgreSql\Statement\Table\Index\ColumnKey;
9use SqlSemantics\Platform\PostgreSql\Statement\Table\Index\ExpressionKey;
10
11/**
12 * Checks the key terms of extended statistics.
13 *
14 * Rule: PG-KEY-TERMS-001. A key term is a column written by name or an
15 * expression; there is at least one. Source:
16 * https://www.postgresql.org/docs/17/sql-createstatistics.html. Status: Implemented.
17 *
18 * @visibility SqlSemantics\Platform\PostgreSql
19 */
20final class KeyTerms
21{
22    /**
23     * Checks a list of key terms.
24     *
25     * @param array<array-key, object|scalar|null> $terms
26     * @return non-empty-list<ColumnKey|ExpressionKey>
27     */
28    public function checked(array $terms): array
29    {
30        $checked = [];
31        foreach (Check::listOf($terms, \SqlSemantics\Platform\PostgreSql\Model\Statement\Node::class, 'Key terms are an ordered list.') as $term) {
32            Check::input($term instanceof ColumnKey || $term instanceof ExpressionKey, 'A key term is a column or an expression.');
33            $checked[] = $term;
34        }
35        Check::input($checked !== [], 'There is at least one key term.');
36
37        return $checked;
38    }
39}
40