Passed
Push — feature-FRAM-112-closure-filte... ( c8f14f )
by Vincent
06:52
created

LikeValue   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A contains() 0 3 1
A startsWith() 0 3 1
A create() 0 7 2
A get() 0 3 1
A endsWith() 0 3 1
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