Comparison::getFilter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
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\Query\Expr\Comparison as DoctrineComparison;
17
use Doctrine\ORM\QueryBuilder;
18
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException;
19
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
20
use Happyr\DoctrineSpecification\Operand\Operand;
21
22
/**
23
 * Comparison class.
24
 *
25
 * This is used when you need to compare two values
26
 *
27
 * @deprecated This class will be marked as abstract in 2.0.
28
 */
29
class Comparison implements Filter
30
{
31
    const EQ = '=';
32
33
    const NEQ = '<>';
34
35
    const LT = '<';
36
37
    const LTE = '<=';
38
39
    const GT = '>';
40
41
    const GTE = '>=';
42
43
    /**
44
     * @deprecated This property will be marked as private in 2.0.
45
     *
46
     * @var Operand|string
47
     */
48
    protected $field;
49
50
    /**
51
     * @deprecated This property will be marked as private in 2.0.
52
     *
53
     * @var Operand|string
54
     */
55
    protected $value;
56
57
    /**
58
     * @deprecated This property will be marked as private in 2.0.
59
     *
60
     * @var string|null
61
     */
62
    protected $dqlAlias;
63
64
    /**
65
     * @var array
66
     */
67
    private static $operators = [
68
        self::EQ, self::NEQ,
69
        self::LT, self::LTE,
70
        self::GT, self::GTE,
71
    ];
72
73
    /**
74
     * @var string
75
     */
76
    private $operator;
77
78
    /**
79
     * Make sure the $field has a value equals to $value.
80
     *
81
     * @param string         $operator
82
     * @param Operand|string $field
83
     * @param Operand|mixed  $value
84
     * @param string|null    $dqlAlias
85
     *
86
     * @throws InvalidArgumentException
87
     */
88
    public function __construct($operator, $field, $value, $dqlAlias = null)
89
    {
90
        if (!in_array($operator, self::$operators, true)) {
91
            throw new InvalidArgumentException(sprintf(
92
                '"%s" is not a valid comparison operator. Valid operators are: "%s"',
93
                $operator,
94
                implode(', ', self::$operators)
95
            ));
96
        }
97
98
        $this->operator = $operator;
99
        $this->field = $field;
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...lter\Comparison::$field has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
100
        $this->value = $value;
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...lter\Comparison::$value has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
101
        $this->dqlAlias = $dqlAlias;
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...r\Comparison::$dqlAlias has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
102
    }
103
104
    /**
105
     * @param QueryBuilder $qb
106
     * @param string       $dqlAlias
107
     *
108
     * @return string
109
     */
110
    public function getFilter(QueryBuilder $qb, $dqlAlias)
111
    {
112
        if (null !== $this->dqlAlias) {
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...r\Comparison::$dqlAlias has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
113
            $dqlAlias = $this->dqlAlias;
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...r\Comparison::$dqlAlias has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
114
        }
115
116
        $field = ArgumentToOperandConverter::toField($this->field);
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...lter\Comparison::$field has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
117
        $value = ArgumentToOperandConverter::toValue($this->value);
0 ignored issues
show
Deprecated Code introduced by
The property Happyr\DoctrineSpecifica...lter\Comparison::$value has been deprecated with message: This property will be marked as private in 2.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
118
119
        return (string) new DoctrineComparison(
120
            $field->transform($qb, $dqlAlias),
121
            $this->operator,
122
            $value->transform($qb, $dqlAlias)
123
        );
124
    }
125
}
126