Deck   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 49
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 3 1
A addRaw() 0 4 1
A add() 0 3 1
A empty() 0 3 1
A format() 0 9 3
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