Deck::addRaw()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace HexMakina\Crudites\Grammar;
4
5
class Deck extends Grammar
6
{
7
    // all the columns, functions and expressions to be aggregated
8
    private string $aggregates;
9
10
    /**
11
     * If the aggregate is a string, it will not be altered (functions, expressions, etc)
12
     * If the aggregate is an array, 
13
     *  - [column], will be backticked as `column`
14
     *  - [table, column], will be backticked as `table`.`column`
15
     * @param $aggregate, 
16
     * @param string $alias, if set, will be backticked
17
     * 
18
     */
19
    public function __construct($aggregate, string $alias = null)
20
    {
21
        $this->aggregates = $this->format($aggregate, $alias);
22
    }
23
24
    public function add($aggregate, string $alias = null): self
25
    {
26
        return $this->addRaw($this->format($aggregate, $alias));
27
    }
28
29
    public function addRaw(string $raw): self
30
    {
31
        $this->aggregates .= ',' . $raw;
32
        return $this;
33
    }
34
35
    public function __toString(): string
36
    {
37
        return $this->aggregates;
38
    }
39
40
    public function empty(): bool
41
    {
42
        return empty($this->aggregates);
43
    }
44
    
45
    protected function format($aggregate, string $alias = null): string
46
    {
47
        $ret = is_string($aggregate) ? $aggregate : self::identifier($aggregate);
48
49
        if ($alias !== null) {
50
            $ret .= ' AS ' . self::identifier($alias);
51
        }
52
53
        return $ret;
54
    }
55
}
56