|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\DB\Fluent\Text; |
|
4
|
|
|
|
|
5
|
|
|
use Helix\DB\Fluent\Num; |
|
6
|
|
|
use Helix\DB\Fluent\Num\BaseConversionTrait; |
|
7
|
|
|
use Helix\DB\Fluent\Text; |
|
8
|
|
|
use Helix\DB\Fluent\Value\ValueTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Produces text related expressions for the instance. |
|
12
|
|
|
*/ |
|
13
|
|
|
trait TextTrait { |
|
14
|
|
|
|
|
15
|
|
|
use ValueTrait; |
|
16
|
|
|
use BaseConversionTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Hex representation. |
|
20
|
|
|
* |
|
21
|
|
|
* `HEX($this)` |
|
22
|
|
|
* |
|
23
|
|
|
* @return Text |
|
24
|
|
|
*/ |
|
25
|
|
|
public function hex () { |
|
26
|
|
|
return Text::factory($this->db, "HEX({$this})"); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Number of characters (not necessarily bytes). |
|
31
|
|
|
* |
|
32
|
|
|
* @see TextTrait::size() |
|
33
|
|
|
* |
|
34
|
|
|
* @return Num |
|
35
|
|
|
*/ |
|
36
|
|
|
public function length () { |
|
37
|
|
|
if ($this->db->isSQLite()) { |
|
38
|
|
|
return Num::factory($this->db, "LENGTH(CAST({$this} AS TEXT))"); |
|
39
|
|
|
} |
|
40
|
|
|
return Num::factory($this->db, "CHAR_LENGTH({$this})"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Lowercase. |
|
45
|
|
|
* |
|
46
|
|
|
* `LOWER($this)` |
|
47
|
|
|
* |
|
48
|
|
|
* @return Text |
|
49
|
|
|
*/ |
|
50
|
|
|
public function lower () { |
|
51
|
|
|
return Text::factory($this->db, "LOWER({$this})"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Substring's position (1-based). |
|
56
|
|
|
* |
|
57
|
|
|
* The position is `0` if the substring isn't found. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $substring |
|
60
|
|
|
* @return Num |
|
61
|
|
|
*/ |
|
62
|
|
|
public function position (string $substring) { |
|
63
|
|
|
$substring = $this->db->quote($substring); |
|
64
|
|
|
if ($this->db->isSQLite()) { |
|
65
|
|
|
return Num::factory($this->db, "INSTR({$this},{$substring})"); |
|
66
|
|
|
} |
|
67
|
|
|
return Num::factory($this->db, "LOCATE({$substring},{$this})"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* String replacement. |
|
72
|
|
|
* |
|
73
|
|
|
* `REPLACE($this,$search,$replace)` |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $search |
|
76
|
|
|
* @param string $replace |
|
77
|
|
|
* @return Text |
|
78
|
|
|
*/ |
|
79
|
|
|
public function replace (string $search, string $replace) { |
|
80
|
|
|
$search = $this->db->quote($search); |
|
81
|
|
|
$replace = $this->db->quote($replace); |
|
82
|
|
|
return Text::factory($this->db, "REPLACE({$this},{$search},{$replace})"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Size in bytes. |
|
87
|
|
|
* |
|
88
|
|
|
* @return Num |
|
89
|
|
|
*/ |
|
90
|
|
|
public function size () { |
|
91
|
|
|
if ($this->db->isSQLite()) { |
|
92
|
|
|
return Num::factory($this->db, "LENGTH(CAST({$this} AS BLOB))"); |
|
93
|
|
|
} |
|
94
|
|
|
return Num::factory($this->db, "LENGTH({$this})"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Substring. |
|
99
|
|
|
* |
|
100
|
|
|
* `SUBSTR($this,$start)` or `SUBSTR($this,$start,$length)` |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $start 1-based, can be negative to start from the right. |
|
103
|
|
|
* @param null|int $length |
|
104
|
|
|
* @return Text |
|
105
|
|
|
*/ |
|
106
|
|
|
public function substr (int $start, int $length = null) { |
|
107
|
|
|
if (isset($length)) { |
|
108
|
|
|
return Text::factory($this->db, "SUBSTR({$this},{$start},{$length})"); |
|
109
|
|
|
} |
|
110
|
|
|
return Text::factory($this->db, "SUBSTR({$this},{$start})"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Convert from an arbitrary base to base 10. |
|
115
|
|
|
* |
|
116
|
|
|
* `CONV($this,$from,10)` |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $from |
|
119
|
|
|
* @return Num |
|
120
|
|
|
*/ |
|
121
|
|
|
public function toBase10 (int $from) { |
|
122
|
|
|
return Num::factory($this->db, "CONV({$this},{$from},10)"); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Uppercase. |
|
127
|
|
|
* |
|
128
|
|
|
* `UPPER($this)` |
|
129
|
|
|
* |
|
130
|
|
|
* @return Text |
|
131
|
|
|
*/ |
|
132
|
|
|
public function upper () { |
|
133
|
|
|
return Text::factory($this->db, "UPPER({$this})"); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|