BaseConversionTrait::toBase2()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
rs 10
1
<?php
2
3
namespace Helix\DB\Fluent\Num;
4
5
use Helix\DB\Fluent\AbstractTrait;
6
use Helix\DB\Fluent\Str;
7
8
/**
9
 * Further manipulate the expression in a different numeric base.
10
 *
11
 * Since converting to base 10 only applies to character string expressions,
12
 * its method can be found at {@link \Helix\DB\Fluent\Str\StrTrait::toBase10()}
13
 */
14
trait BaseConversionTrait
15
{
16
17
    use AbstractTrait;
18
19
    /**
20
     * Convert between arbitrary bases.
21
     *
22
     * `CONV($this,$from,$to)`
23
     *
24
     * @param int $from
25
     * @param int $to
26
     * @return Str
27
     */
28
    public function toBase(int $from, int $to)
29
    {
30
        return Str::factory($this->db, "CONV({$this},{$from},{$to})");
31
    }
32
33
    /**
34
     * Convert from an arbitrary base to base 16.
35
     *
36
     * `CONV($this,$from,16)`
37
     *
38
     * @param int $from
39
     * @return Str
40
     */
41
    public function toBase16(int $from = 10)
42
    {
43
        return $this->toBase($from, 16);
44
    }
45
46
    /**
47
     * Convert from an arbitrary base to base 2.
48
     *
49
     * `CONV($this,$from,2)`
50
     *
51
     * @param int $from
52
     * @return Str
53
     */
54
    public function toBase2(int $from = 10)
55
    {
56
        return $this->toBase($from, 2);
57
    }
58
59
    /**
60
     * Convert from an arbitrary base to base 8.
61
     *
62
     * `CONV($this,$from,8)`
63
     *
64
     * @param int $from
65
     * @return Str
66
     */
67
    public function toBase8(int $from = 10)
68
    {
69
        return $this->toBase($from, 8);
70
    }
71
}
72