-
Notifications
You must be signed in to change notification settings - Fork 51
/
LikeValue.php
48 lines (42 loc) · 1.14 KB
/
LikeValue.php
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
<?php
declare(strict_types=1);
namespace Latitude\QueryBuilder;
abstract class LikeValue
{
/**
* Escape input for a LIKE condition value.
*/
public static function escape(string $value): string
{
// Backslash is used to escape wildcards.
$value = str_replace('\\', '\\\\', $value);
// Standard wildcards are underscore and percent sign.
$value = str_replace('%', '\\%', $value);
$value = str_replace('_', '\\_', $value);
return $value;
}
/**
* Escape input for a LIKE condition, surrounding with wildcards.
*/
public static function any(string $value): string
{
$value = static::escape($value);
return "%$value%";
}
/**
* Escape input for a LIKE condition, ends with wildcards.
*/
public static function starts(string $value): string
{
$value = static::escape($value);
return "$value%";
}
/**
* Escape input for a LIKE condition, starts with wildcards.
*/
public static function ends(string $value): string
{
$value = static::escape($value);
return "%$value";
}
}