Completed
Push — master ( 40a351...e6533f )
by Woody
11s
created

LikeValue::ends()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder;
5
6
abstract class LikeValue
7
{
8
    /**
9
     * Escape input for a LIKE condition value.
10
     */
11 5
    public static function escape(string $value): string
12
    {
13
        // Backslash is used to escape wildcards.
14 5
        $value = str_replace('\\', '\\\\', $value);
15
        // Standard wildcards are underscore and percent sign.
16 5
        $value = str_replace('%', '\\%', $value);
17 5
        $value = str_replace('_', '\\_', $value);
18
19 5
        return $value;
20
    }
21
22
    /**
23
     * Escape input for a LIKE condition, surrounding with wildcards.
24
     */
25 1
    public static function any(string $value): string
26
    {
27 1
        $value = static::escape($value);
28 1
        return "%$value%";
29
    }
30
31
    /**
32
     * Escape input for a LIKE condition, ends with wildcards.
33
     */
34 1
    public static function starts(string $value): string
35
    {
36 1
        $value = static::escape($value);
37 1
        return "$value%";
38
    }
39
40
    /**
41
     * Escape input for a LIKE condition, starts with wildcards.
42
     */
43 1
    public static function ends(string $value): string
44
    {
45 1
        $value = static::escape($value);
46 1
        return "%$value";
47
    }
48
}
49