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\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
abstract class Arithmetic implements Operand |
20
|
|
|
{ |
21
|
|
|
const ADD = '+'; |
22
|
|
|
|
23
|
|
|
const SUB = '-'; |
24
|
|
|
|
25
|
|
|
const MUL = '*'; |
26
|
|
|
|
27
|
|
|
const DIV = '/'; |
28
|
|
|
|
29
|
|
|
const MOD = '%'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private static $operations = [ |
35
|
|
|
self::ADD, |
36
|
|
|
self::SUB, |
37
|
|
|
self::MUL, |
38
|
|
|
self::DIV, |
39
|
|
|
self::MOD, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $operation = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Operand|string |
49
|
|
|
*/ |
50
|
|
|
private $field; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Operand|string |
54
|
|
|
*/ |
55
|
|
|
private $value; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $operation |
59
|
|
|
* @param Operand|string $field |
60
|
|
|
* @param Operand|string $value |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function __construct($operation, $field, $value) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (!in_array($operation, self::$operations)) { |
65
|
|
|
throw new InvalidArgumentException(sprintf( |
66
|
|
|
'"%s" is not a valid arithmetic operation. Valid operations are: "%s"', |
67
|
|
|
$operation, |
68
|
|
|
implode(', ', self::$operations) |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->operation = $operation; |
73
|
|
|
$this->field = $field; |
74
|
|
|
$this->value = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param QueryBuilder $qb |
79
|
|
|
* @param string $dqlAlias |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function transform(QueryBuilder $qb, $dqlAlias) |
84
|
|
|
{ |
85
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
86
|
|
|
$value = ArgumentToOperandConverter::toValue($this->value); |
87
|
|
|
|
88
|
|
|
$field = $field->transform($qb, $dqlAlias); |
89
|
|
|
$value = $value->transform($qb, $dqlAlias); |
90
|
|
|
|
91
|
|
|
return sprintf('(%s %s %s)', $field, $this->operation, $value); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.