Completed
Push — master ( e6533f...d6c42a )
by Woody
12s
created

Alias::sql()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder;
5
6
class Alias implements Statement
7
{
8
    /**
9
     * Create a new alias.
10
     *
11
     * @param Statement|string $statement
12
     */
13 3
    public static function make($statement, string $alias): Alias
14
    {
15 3
        return new static(reference($statement), $alias);
16
    }
17
18
    // Statement
19 2
    public function sql(Identifier $identifier = null): string
20
    {
21 2
        return sprintf(
22 2
            isQuery($this->statement) ? '(%s) AS %s' : '%s AS %s',
23 2
            $this->statement->sql($identifier),
24 2
            $this->alias
25
        );
26
    }
27
28
    // Statement
29 3
    public function params(): array
30
    {
31 3
        return $this->statement->params();
32
    }
33
34
    /**
35
     * @var Statement
36
     */
37
    protected $statement;
38
39
    /**
40
     * @var string
41
     */
42
    protected $alias;
43
44
    /**
45
     * @see Alias::make()
46
     */
47 3
    protected function __construct(Statement $statement, string $alias)
48
    {
49 3
        $this->statement = $statement;
50 3
        $this->alias = $alias;
51 3
    }
52
}
53