MemberOfX::getFilter()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
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\Filter;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
18
use Happyr\DoctrineSpecification\Operand\Operand;
19
20
class MemberOfX implements Filter
21
{
22
    /**
23
     * @var Operand|string
24
     */
25
    private $field;
26
27
    /**
28
     * @var Operand|string
29
     */
30
    private $value;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $dqlAlias;
36
37
    /**
38
     * @param Operand|string $value
39
     * @param Operand|string $field
40
     * @param string|null    $dqlAlias
41
     */
42
    public function __construct($value, $field, $dqlAlias = null)
43
    {
44
        $this->value = $value;
45
        $this->field = $field;
46
        $this->dqlAlias = $dqlAlias;
47
    }
48
49
    /**
50
     * @param QueryBuilder $qb
51
     * @param string       $dqlAlias
52
     *
53
     * @return string
54
     */
55
    public function getFilter(QueryBuilder $qb, $dqlAlias)
56
    {
57
        if (null !== $this->dqlAlias) {
58
            $dqlAlias = $this->dqlAlias;
59
        }
60
61
        $field = ArgumentToOperandConverter::toField($this->field);
62
        $value = ArgumentToOperandConverter::toValue($this->value);
63
64
        // doctrine/orm < 2.5
65
        if (!method_exists($qb, 'isMemberOf')) {
66
            return sprintf('%s MEMBER OF %s', $value->transform($qb, $dqlAlias), $field->transform($qb, $dqlAlias));
67
        }
68
69
        return (string) $qb->expr()->isMemberOf(
70
            $value->transform($qb, $dqlAlias),
71
            $field->transform($qb, $dqlAlias)
72
        );
73
    }
74
}
75