packages/sql-semantics-postgres/src/Statement/Routine/ParameterMode.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Routine;
6
7/**
8 * The written mode of a routine parameter.
9 *
10 * Mirrors PostgreSQL's `FunctionParameterMode` as written: `INOUT` and
11 * `IN OUT` are the same mode spelled with one or two keywords, and both are
12 * kept so that the statement is written back as it was. A parameter written
13 * without a mode is an input parameter.
14 * Source: https://www.postgresql.org/docs/17/sql-createfunction.html.
15 *
16 * @visibility public
17 * @example Telling the input modes
18 *     [\SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode::InAndOut->input(), \SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode::Out->input()] // => [true, false]
19 */
20enum ParameterMode: string
21{
22    case In = 'IN';
23    case Out = 'OUT';
24    case InOut = 'INOUT';
25    case InAndOut = 'IN OUT';
26    case Variadic = 'VARIADIC';
27
28    /**
29     * Tells whether the caller passes a value for the parameter.
30     */
31    public function input(): bool
32    {
33        return $this !== self::Out;
34    }
35
36    /**
37     * Tells whether the parameter is part of the result.
38     */
39    public function output(): bool
40    {
41        return $this === self::Out || $this === self::InOut || $this === self::InAndOut;
42    }
43
44    /**
45     * Answers the keywords that spell the mode.
46     *
47     * @return non-empty-list<string>
48     */
49    public function keywords(): array
50    {
51        return explode(' ', $this->value);
52    }
53}
54