packages/sql-formatter/src/Core/Syntax/Expressions.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlFormatter\Core\Syntax;
6
7/**
8 * Distinguishes boolean conjunctions, range predicates, signs, and CASE expressions.
9 *
10 * @visibility SqlFormatter
11 */
12final class Expressions
13{
14 /**
15 * Shares the document being annotated.
16 */
17 public function __construct(private readonly Document $document, private readonly Rules $rules)
18 {
19 }
20
21 /**
22 * @param array<int, string> $direct
23 */
24 public function mark(string $name, int $start, int $end, array $direct): void
25 {
26 if (!$this->rules->has('expressions', $name)) {
27 return;
28 }
29 $between = in_array('BETWEEN', array_map(strtoupper(...), $direct), true);
30 foreach ($direct as $index => $text) {
31 $word = strtoupper($text);
32 if (in_array($word, ['AND', 'OR', 'XOR'], true) && !$between) {
33 $this->document->logical[$index] = true;
34 }
35 if ($index === $start && in_array($text, ['+', '-', '~', '!'], true)) {
36 $this->document->unary[$index] = true;
37 }
38 if ($word === 'CASE') {
39 $this->document->casePairs[$index] = $end;
40 }
41 }
42 }
43}
44