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

LikePattern::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
c 0
b 0
f 0
rs 9.9666
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Operand;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Happyr\DoctrineSpecification\ValueConverter;
7
8
class LikePattern implements Operand
9
{
10
    const CONTAINS = '%%%s%%';
11
12
    const ENDS_WITH = '%%%s';
13
14
    const STARTS_WITH = '%s%%';
15
16
    /**
17
     * @var string
18
     */
19
    private $value;
20
21
    /**
22
     * @var string
23
     */
24
    private $format;
25
26
    /**
27
     * @param string $value
28
     * @param string $format
29
     */
30
    public function __construct($value, $format = self::CONTAINS)
31
    {
32
        $this->value = $value;
33
        $this->format = $format;
34
    }
35
36
    /**
37
     * @param QueryBuilder $qb
38
     * @param string       $dqlAlias
39
     *
40
     * @return string
41
     */
42 View Code Duplication
    public function transform(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...
43
    {
44
        $paramName = sprintf('comparison_%d', $qb->getParameters()->count());
45
        $value = ValueConverter::convertToDatabaseValue($this->value, $qb);
46
        $value = $this->formatValue($this->format, $value);
47
        $qb->setParameter($paramName, $value);
48
49
        return sprintf(':%s', $paramName);
50
    }
51
52
    /**
53
     * @param string $format
54
     * @param string $value
55
     *
56
     * @return string
57
     */
58
    private function formatValue($format, $value)
59
    {
60
        return sprintf($format, $value);
61
    }
62
}
63