Passed
Push — master ( 1dcf9b...c6a312 )
by y
01:25
created

CastTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
dl 0
loc 50
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toFloat() 0 5 2
A toText() 0 5 2
A coalesce() 0 4 1
A toInt() 0 5 2
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
trait CastTrait {
6
7
    use AbstractTrait;
8
9
    /**
10
     * `COALESCE($this, ...$values)`
11
     *
12
     * @param scalar[] $values
13
     * @return Value
14
     */
15
    public function coalesce (array $values) {
16
        array_unshift($values, $this);
17
        $values = $this->db->quoteList($values);
18
        return Value::factory($this->db, "COALESCE({$values})");
19
    }
20
21
    /**
22
     * Casts the expression to a floating point number.
23
     *
24
     * @return Num
25
     */
26
    public function toFloat () {
27
        if ($this->db->isSQLite()) {
28
            return Num::factory($this->db, "CAST({$this} AS REAL)");
29
        }
30
        return Num::factory($this->db, "({$this} + 0)");
31
    }
32
33
    /**
34
     * Casts the expression to a signed integer.
35
     *
36
     * @return Num
37
     */
38
    public function toInt () {
39
        if ($this->db->isSQLite()) {
40
            return Num::factory($this->db, "CAST({$this} AS INTEGER)");
41
        }
42
        return Num::factory($this->db, "CAST({$this} AS SIGNED)");
43
    }
44
45
    /**
46
     * Casts the expression to a character string.
47
     *
48
     * @return Text
49
     */
50
    public function toText () {
51
        if ($this->db->isSQLite()) {
52
            return Text::factory($this->db, "CAST({$this} AS TEXT)");
53
        }
54
        return Text::factory($this->db, "CAST({$this} AS CHAR)");
55
    }
56
}