LikePattern::transform()   A
last analyzed

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
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification\Operand;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Happyr\DoctrineSpecification\ValueConverter;
18
19
class LikePattern implements Operand
20
{
21
    const CONTAINS = '%%%s%%';
22
23
    const ENDS_WITH = '%%%s';
24
25
    const STARTS_WITH = '%s%%';
26
27
    /**
28
     * @var string
29
     */
30
    private $value;
31
32
    /**
33
     * @var string
34
     */
35
    private $format;
36
37
    /**
38
     * @param string $value
39
     * @param string $format
40
     */
41
    public function __construct($value, $format = self::CONTAINS)
42
    {
43
        $this->value = $value;
44
        $this->format = $format;
45
    }
46
47
    /**
48
     * @param QueryBuilder $qb
49
     * @param string       $dqlAlias
50
     *
51
     * @return string
52
     */
53 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...
54
    {
55
        $paramName = sprintf('comparison_%d', $qb->getParameters()->count());
56
        $value = ValueConverter::convertToDatabaseValue($this->value, $qb);
57
        $value = $this->formatValue($this->format, $value);
58
        $qb->setParameter($paramName, $value);
59
60
        return sprintf(':%s', $paramName);
61
    }
62
63
    /**
64
     * @param string $format
65
     * @param string $value
66
     *
67
     * @return string
68
     */
69
    private function formatValue($format, $value)
70
    {
71
        return sprintf($format, $value);
72
    }
73
}
74