Passed
Push — master ( c6a312...8ed35d )
by y
05:46
created

CastTrait::toBase16()   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
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
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
     * `CONV($this,$from,$to)`
23
     *
24
     * @param int $from
25
     * @param int $to
26
     * @return Text
27
     */
28
    public function toBase (int $from, int $to) {
29
        return Text::factory($this->db, "CONV({$this},{$from},{$to})");
30
    }
31
32
    /**
33
     * `CONV($this,$from,10)`
34
     *
35
     * @param int $from
36
     * @return Num
37
     */
38
    public function toBase10 (int $from) {
39
        return Num::factory($this->db, "CONV({$this},{$from},10)");
40
    }
41
42
    /**
43
     * `CONV($this,$from,16)`
44
     *
45
     * This is similar to {@link TextTrait::hex()} except you can specify the starting base.
46
     *
47
     * @param int $from
48
     * @return Text
49
     */
50
    public function toBase16 (int $from) {
51
        return $this->toBase($from, 16);
52
    }
53
54
    /**
55
     * `CONV($this,$from,2)`
56
     *
57
     * @param int $from
58
     * @return Text
59
     */
60
    public function toBase2 (int $from) {
61
        return $this->toBase($from, 2);
62
    }
63
64
    /**
65
     * `CONV($this,$from,8)`
66
     *
67
     * @param int $from
68
     * @return Text
69
     */
70
    public function toBase8 (int $from) {
71
        return $this->toBase($from, 8);
72
    }
73
74
    /**
75
     * Casts the expression to a floating point number.
76
     *
77
     * @return Num
78
     */
79
    public function toFloat () {
80
        if ($this->db->isSQLite()) {
81
            return Num::factory($this->db, "CAST({$this} AS REAL)");
82
        }
83
        return Num::factory($this->db, "({$this} + 0)");
84
    }
85
86
    /**
87
     * Casts the expression to a signed integer.
88
     *
89
     * @return Num
90
     */
91
    public function toInt () {
92
        if ($this->db->isSQLite()) {
93
            return Num::factory($this->db, "CAST({$this} AS INTEGER)");
94
        }
95
        return Num::factory($this->db, "CAST({$this} AS SIGNED)");
96
    }
97
98
    /**
99
     * Casts the expression to a character string.
100
     *
101
     * @return Text
102
     */
103
    public function toText () {
104
        if ($this->db->isSQLite()) {
105
            return Text::factory($this->db, "CAST({$this} AS TEXT)");
106
        }
107
        return Text::factory($this->db, "CAST({$this} AS CHAR)");
108
    }
109
}