Lines 100.00% 7 / 7
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
QuotesLiterals 100.00% 7 / 7 100.00% 1 / 1 6 100.00% 1 / 1
 quoteLiteral 100.00% 7 / 7 100.00% 1 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace BlueprintAU\Radiant\Database\Concerns;
6
7/**
8 * Quotes scalar values as SQL literals.
9 *
10 * Shared by the query {@see \BlueprintAU\Radiant\Database\Grammars\Grammar}
11 * and the schema {@see \BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar} —
12 * two unrelated hierarchies that both need to render a scalar as a SQL
13 * literal (a default value in DDL, an inline value in a query). A trait
14 * (not a base class) because it must reach across both trees.
15 */
16trait QuotesLiterals
17{
18    /**
19     * Quote a scalar as a SQL literal.
20     *
21     * @param  string|int|float|bool|null  $value
22     * @return string
23     */
24    protected function quoteLiteral(string|int|float|bool|null $value): string
25    {
26        if ($value === null) {
27            return 'null';
28        }
29        if (is_bool($value)) {
30            return $value ? '1' : '0';
31        }
32        if (is_int($value) || is_float($value)) {
33            return (string) $value;
34        }
35        return "'" . str_replace("'", "''", $value) . "'";
36    }
37}