Passed
Push — master ( 222f48...0fa176 )
by y
01:47
created

BaseConversionTrait::toBase2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Helix\DB\Fluent\Num;
4
5
use Helix\DB\Fluent\AbstractTrait;
6
use Helix\DB\Fluent\Text;
7
8
/**
9
 * Convert a numeric expression to other bases.
10
 *
11
 * Since converting to base 10 only applies to text expressions,
12
 * its method can be found at {@link Text\TextTrait::toBase10()}
13
 */
14
trait BaseConversionTrait {
15
16
    use AbstractTrait;
17
18
    /**
19
     * Convert between arbitrary bases.
20
     *
21
     * `CONV($this,$from,$to)`
22
     *
23
     * @param int $from
24
     * @param int $to
25
     * @return Text
26
     */
27
    public function toBase (int $from, int $to) {
28
        return Text::factory($this->db, "CONV({$this},{$from},{$to})");
29
    }
30
31
    /**
32
     * Convert from an arbitrary base to base 16.
33
     *
34
     * `CONV($this,$from,16)`
35
     *
36
     * @param int $from
37
     * @return Text
38
     */
39
    public function toBase16 (int $from = 10) {
40
        return $this->toBase($from, 16);
41
    }
42
43
    /**
44
     * Convert from an arbitrary base to base 2.
45
     *
46
     * `CONV($this,$from,2)`
47
     *
48
     * @param int $from
49
     * @return Text
50
     */
51
    public function toBase2 (int $from = 10) {
52
        return $this->toBase($from, 2);
53
    }
54
55
    /**
56
     * Convert from an arbitrary base to base 8.
57
     *
58
     * `CONV($this,$from,8)`
59
     *
60
     * @param int $from
61
     * @return Text
62
     */
63
    public function toBase8 (int $from = 10) {
64
        return $this->toBase($from, 8);
65
    }
66
}
67