Expression::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Expression;
6
7
use Stringable;
8
use Yiisoft\Db\Connection\ConnectionInterface;
9
10
/**
11
 * Represents a DB expression that doesn't need escaping or quoting.
12
 *
13
 * When an Expression object is embedded within a SQL statement or fragment, it will be replaced with the
14
 * {@see expression} property value without any DB escaping or quoting.
15
 *
16
 * For example,
17
 *
18
 * ```php
19
 * $expression = new Expression('NOW()');
20
 * $now = (new \Yiisoft\Db\Query\Query)->select($expression)->scalar();  // SELECT NOW();
21
 * echo $now; // prints the current date
22
 * ```
23
 *
24
 * Expression objects are mainly created for passing raw SQL expressions to methods of
25
 * {@see \Yiisoft\Db\Query\QueryInterface} and related classes.
26
 *
27 202
 * @psalm-import-type ParamsType from ConnectionInterface
28
 */
29 202
class Expression implements ExpressionInterface, Stringable
30 202
{
31 202
    /**
32
     * @psalm-param ParamsType $params
33
     */
34
    public function __construct(private string $expression, private array $params = [])
35
    {
36
    }
37
38 369
    /**
39
     * @return string The expression.
40 369
     */
41
    public function __toString(): string
42
    {
43
        return $this->expression;
44
    }
45
46
    /**
47
     * @return array List of parameters to bind to this expression. The keys are placeholders appearing in
48
     * {@see expression} and the values are the corresponding parameter values.
49
     *
50 333
     * @psalm-return ParamsType
51
     */
52 333
    public function getParams(): array
53
    {
54
        return $this->params;
55
    }
56
}
57