Completed
Push — 1.0 ( b7886a...4c0b42 )
by Peter
07:48 queued 11s
created

Like::getFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
c 0
b 0
f 0
rs 9.8333
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Filter;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Doctrine\ORM\Query\Expr\Comparison as DoctrineComparison;
7
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
8
use Happyr\DoctrineSpecification\Operand\LikePattern;
9
use Happyr\DoctrineSpecification\Operand\Operand;
10
11
class Like implements Filter
12
{
13
    const CONTAINS = LikePattern::CONTAINS;
14
15
    const ENDS_WITH = LikePattern::ENDS_WITH;
16
17
    const STARTS_WITH = LikePattern::STARTS_WITH;
18
19
    /**
20
     * @var Operand|string
21
     */
22
    private $field;
23
24
    /**
25
     * @var LikePattern
26
     */
27
    private $value;
28
29
    /**
30
     * @var string
31
     */
32
    private $dqlAlias;
33
34
    /**
35
     * @param Operand|string     $field
36
     * @param LikePattern|string $value
37
     * @param string             $format
38
     * @param string|null        $dqlAlias
39
     */
40
    public function __construct($field, $value, $format = LikePattern::CONTAINS, $dqlAlias = null)
41
    {
42
        if (!($value instanceof LikePattern)) {
43
            $value = new LikePattern($value, $format);
44
        }
45
        $this->field = $field;
46
        $this->value = $value;
47
        $this->dqlAlias = $dqlAlias;
48
    }
49
50
    /**
51
     * @param QueryBuilder $qb
52
     * @param string       $dqlAlias
53
     *
54
     * @return string
55
     */
56 View Code Duplication
    public function getFilter(QueryBuilder $qb, $dqlAlias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if (null !== $this->dqlAlias) {
59
            $dqlAlias = $this->dqlAlias;
60
        }
61
62
        $field = ArgumentToOperandConverter::toField($this->field);
63
64
        $field = $field->transform($qb, $dqlAlias);
65
        $value = $this->value->transform($qb, $dqlAlias);
66
67
        return (string) new DoctrineComparison($field, 'LIKE', $value);
68
    }
69
}
70