packages/sql-semantics-postgres/src/Statement/Invocation/Xml/XmlElement.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
9use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
11use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Rules\Invocation\XmlChecks;
19use SqlSemantics\Platform\PostgreSql\Statement\OutputNaming;
20use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
21
22/**
23 * `XMLELEMENT (NAME name [, XMLATTRIBUTES (attributes)] [, content])`: an XML element.
24 *
25 * Mirrors PostgreSQL's `XmlExpr` of kind `IS_XMLELEMENT`. Rule:
26 * PG-XMLELEMENT-001. Facts: `xml`, not NULL. Diagnostics: an unnamed
27 * attribute that is not a column reference, and an attribute name used
28 * twice (PG-XML-CHECKS-001). The result column is named `xmlelement`.
29 * Source: https://www.postgresql.org/docs/17/functions-xml.html#FUNCTIONS-PRODUCING-XML-XMLELEMENT. Status: Implemented.
30 *
31 * @visibility public
32 * @example Reading an empty element
33 *     $element = new \SqlSemantics\Platform\PostgreSql\Statement\Invocation\Xml\XmlElement(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('br'));
34 *     [$element->name->value, $element->attributes, $element->content] // => ['br', [], []]
35 */
36final class XmlElement implements Scalar, OutputNaming
37{
38    use Snapshot;
39
40    /**
41     * @var list<XmlAttribute> The attributes; none when XMLATTRIBUTES is not written
42     */
43    public readonly array $attributes;
44
45    /**
46     * @var list<Scalar> The content values
47     */
48    public readonly array $content;
49
50    /**
51     * @param Name $name The element name
52     * @param list<XmlAttribute> $attributes The attributes; none when XMLATTRIBUTES is not written
53     * @param list<Scalar> $content The content values
54     */
55    public function __construct(public readonly Name $name, array $attributes = [], array $content = [])
56    {
57        $this->attributes = Check::listOf($attributes, XmlAttribute::class, 'XML attributes are attribute values.');
58        $this->content = Check::listOf($content, Scalar::class, 'XML element content is a list of values.');
59    }
60
61    /**
62     * Names an unaliased result column.
63     */
64    public function outputName(): Name
65    {
66        return new Name('xmlelement');
67    }
68
69    /**
70     * Derives the attributes and the content, and reports misnamed attributes.
71     */
72    public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
73    {
74        foreach ($this->attributes as $attribute) {
75            $attribute->deriveClause($derivation, $environment);
76        }
77        foreach ($this->content as $value) {
78            $derivation->scalar($value, $environment);
79        }
80        (new XmlChecks())->attributes($derivation, $this->attributes);
81
82        return new ScalarFact(new Known(Builtin::Xml), Nullability::NotNull);
83    }
84
85    /**
86     * Writes XMLELEMENT with the name, the attributes and the content.
87     */
88    public function render(Output $out): void
89    {
90        $out->keyword('XMLELEMENT')->glue()->symbol('(')->keyword('NAME')->name($this->name, NameUse::Label);
91        if ($this->attributes !== []) {
92            $out->symbol(',')->keyword('XMLATTRIBUTES')->glue()->symbol('(')->list($this->attributes)->symbol(')');
93        }
94        if ($this->content !== []) {
95            $out->symbol(',')->list($this->content);
96        }
97        $out->symbol(')');
98    }
99}
100