|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Closure\Value; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionFunction; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Apply transformation on value for LIKE comparison |
|
9
|
|
|
*/ |
|
10
|
|
|
final class LikeValue implements ComparisonValueInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private ComparisonValueInterface $value; |
|
13
|
|
|
private string $prefix; |
|
14
|
|
|
private string $suffix; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param ComparisonValueInterface $value Base value accessor |
|
18
|
|
|
* @param string $prefix Prefix to prepend |
|
19
|
|
|
* @param string $suffix Suffix to append |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public function __construct(ComparisonValueInterface $value, string $prefix, string $suffix) |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->value = $value; |
|
24
|
2 |
|
$this->prefix = $prefix; |
|
25
|
2 |
|
$this->suffix = $suffix; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function get(ReflectionFunction $reflection) |
|
32
|
|
|
{ |
|
33
|
2 |
|
return $this->prefix . addcslashes($this->value->get($reflection), '%_') . $this->suffix; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
6 |
|
public static function create(ComparisonValueInterface $value, string $prefix, string $suffix): ComparisonValueInterface |
|
37
|
|
|
{ |
|
38
|
6 |
|
if ($value instanceof ConstantValue) { |
|
39
|
4 |
|
return new ConstantValue($prefix . addcslashes($value->value(), '%_') . $suffix); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
return new self($value, $prefix, $suffix); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
public static function startsWith(ComparisonValueInterface $value): ComparisonValueInterface |
|
46
|
|
|
{ |
|
47
|
5 |
|
return self::create($value, '', '%'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
public static function endsWith(ComparisonValueInterface $value): ComparisonValueInterface |
|
51
|
|
|
{ |
|
52
|
3 |
|
return self::create($value, '%', ''); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
public static function contains(ComparisonValueInterface $value): ComparisonValueInterface |
|
56
|
|
|
{ |
|
57
|
3 |
|
return self::create($value, '%', '%'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|