Passed
Push — master ( 9b561d...08d227 )
by y
01:25
created

AggregateTrait::getMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
use Helix\DB;
6
7
/**
8
 * Produces aggregate expressions for the instance.
9
 */
10
trait AggregateTrait {
11
12
    /**
13
     * @var DB
14
     */
15
    protected $db;
16
17
    /**
18
     * `AVG(ALL|DISTINCT $this)`
19
     *
20
     * @param string $aggregate `ALL|DISTINCT`
21
     * @return Numeric
22
     */
23
    public function getAvg ($aggregate = 'ALL') {
24
        return new Numeric($this->db, "AVG({$aggregate} {$this})");
25
    }
26
27
    /**
28
     * `GROUP_CONCAT($this)` using a delimiter.
29
     *
30
     * @param string $delimiter
31
     * @return Text
32
     */
33
    public function getConcat (string $delimiter = ',') {
34
        $delimiter = $this->db->quote($delimiter);
35
        switch ($this->db->getDriver()) {
36
            case 'sqlite':
37
                return new Text($this->db, "GROUP_CONCAT({$this},{$delimiter})");
38
            default:
39
                return new Text($this->db, "GROUP_CONCAT({$this} SEPARATOR {$delimiter})");
40
        }
41
    }
42
43
    /**
44
     * `COUNT(ALL|DISTINCT $this)`
45
     *
46
     * @param string $aggregate `ALL|DISTINCT`
47
     * @return Numeric
48
     */
49
    public function getCount ($aggregate = 'ALL') {
50
        return new Numeric($this->db, "COUNT({$aggregate} {$this})");
51
    }
52
53
    /**
54
     * `MAX($this)`
55
     *
56
     * @return Numeric
57
     */
58
    public function getMax () {
59
        return new Numeric($this->db, "MAX({$this})");
60
    }
61
62
    /**
63
     * `MIN($this)`
64
     *
65
     * @return Numeric
66
     */
67
    public function getMin () {
68
        return new Numeric($this->db, "MIN({$this})");
69
    }
70
71
    /**
72
     * `SUM(ALL|DISTINCT $this)`
73
     *
74
     * @param string $aggregate `ALL|DISTINCT`
75
     * @return Numeric
76
     */
77
    public function getSum ($aggregate = 'ALL') {
78
        return new Numeric($this->db, "SUM({$aggregate} {$this})");
79
    }
80
}