-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLike.php
More file actions
169 lines (145 loc) · 2.9 KB
/
Copy pathLike.php
File metadata and controls
169 lines (145 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Bdf\Prime\Query\Expression;
use Bdf\Prime\Types\ArrayType;
/**
* Generate LIKE pattern on expressions
*
* <code>
* $query->where('roles', (new Like([1, 2]))->searchableArray()); // roles LIKE '%,1,%' OR roles LIKE '%,2,%'
* $query->where('name', (new Like('John'))->contains()); // name LIKE '%John%'
* $query->where('name', (new Like('John'))->startsWith()); // name LIKE 'John%'
* </code>
*/
class Like extends AbstractExpressionTransformer
{
/**
* @var string
*/
protected $start = '';
/**
* @var string
*/
protected $end = '';
/**
* @var bool
*/
protected $escape = false;
/**
* Set the starting string
*
* @param string $start
*
* @return $this
*/
public function start($start = '%')
{
$this->start = (string) $start;
return $this;
}
/**
* Set the ending string
*
* @param string $end
*
* @return $this
*/
public function end($end = '%')
{
$this->end = (string) $end;
return $this;
}
/**
* Enclose the pattern
*
* @param string $char
*
* @return $this
*/
public function enclose($char = '%')
{
$this->start = $char;
$this->end = $char;
return $this;
}
/**
* Escape the value
*
* @param bool $escape
*
* @return $this
*/
public function escape($escape = true)
{
$this->escape = (bool) $escape;
return $this;
}
/**
* Search attributes that contains the value
*
* @return $this
*/
public function contains()
{
return $this->enclose('%');
}
/**
* Search attributes that starts with the value
*
* @return $this
*/
public function startsWith()
{
return $this->end('%');
}
/**
* Search attributes that ends with the value
*
* @return $this
*/
public function endsWith()
{
return $this->start('%');
}
/**
* Perform LIKE query on @see ArrayType attribute
*
* @return $this
*/
public function searchableArray()
{
$this->start = '%,';
$this->end = ',%';
return $this;
}
/**
* {@inheritdoc}
*/
public function getValue()
{
if (is_array($this->value)) {
return array_map([$this, 'generate'], $this->value);
}
return $this->generate($this->value);
}
/**
* {@inheritdoc}
*/
public function getOperator(): string
{
return ':like';
}
/**
* Generate the LIKE pattern
*
* @param string $value
*
* @return string
*/
public function generate($value)
{
if ($this->escape) {
$value = addcslashes($value, '%_');
}
return $this->start.$value.$this->end;
}
}