packages/sql-semantics-postgres/src/Rules/Utility/OptionArguments.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Rules\Utility;
6
7use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
8use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
9use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
10use SqlSemantics\Platform\PostgreSql\Statement\Option\Toggle;
11use SqlSemantics\Platform\PostgreSql\Statement\Option\Word;
12use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\UtilityOption;
13
14/**
15 * Reads the value of a utility option the way the server does.
16 *
17 * Rule: PG-UTILITY-OPTION-VALUE-001. A Boolean option without a value is
18 * true. With a value, the integers 0 and 1 are false and true, and a word,
19 * a string or one of the keywords TRUE, FALSE and ON is true for `true` and
20 * `on` and false for `false` and `off`, compared without regard to case;
21 * any other value is not Boolean. An integer option takes an integer
22 * constant that fits in 32 bits: the lexer reads a larger integer as a
23 * decimal number, which is not an integer. When an option is written more
24 * than once, the last occurrence decides. Termination: one pass over a
25 * finite list.
26 * Source: https://www.postgresql.org/docs/17/sql-explain.html, https://www.postgresql.org/docs/17/sql-vacuum.html,
27 * https://www.postgresql.org/docs/17/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-NUMERIC.
28 * Status: Implemented.
29 *
30 * @visibility SqlSemantics\Platform\PostgreSql
31 */
32final class OptionArguments
33{
34    /**
35     * Answers the Boolean value of an option, or null when its value is not Boolean.
36     */
37    public function boolean(UtilityOption $option): ?bool
38    {
39        $argument = $option->argument;
40        if ($argument === null) {
41            return true;
42        }
43        if ($argument instanceof SignedNumber) {
44            $integer = $this->integer($option);
45
46            return match ($integer) {
47                0 => false,
48                1 => true,
49                default => null,
50            };
51        }
52        $text = match (true) {
53            $argument instanceof Toggle => $argument->text(),
54            $argument instanceof Word => $argument->word->value,
55            $argument instanceof StringConstant => $argument->value,
56        };
57
58        return match (strtolower($text)) {
59            'true', 'on' => true,
60            'false', 'off' => false,
61            default => null,
62        };
63    }
64
65    /**
66     * Answers the integer value of an option, or null when it has no value or the value is not an integer that fits in 32 bits.
67     */
68    public function integer(UtilityOption $option): ?int
69    {
70        $argument = $option->argument;
71        if (!$argument instanceof SignedNumber || !$argument->magnitude instanceof IntegerConstant) {
72            return null;
73        }
74        $digits = $argument->magnitude->digits;
75
76        return $argument->negative ? -(int) $digits : (int) $digits;
77    }
78
79    /**
80     * Answers the Boolean value of the last option of a name, or a default when none is written or its value is not Boolean.
81     *
82     * @param list<UtilityOption> $options
83     */
84    public function enabled(array $options, string $name, bool $default): bool
85    {
86        $option = (new OptionRules())->find($options, $name);
87
88        return $option === null ? $default : ($this->boolean($option) ?? $default);
89    }
90}
91