Completed
Push — master ( a7632b...c1b153 )
by Peter
07:59
created

src/Operand/Arithmetic.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Happyr\DoctrineSpecification\Operand;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException;
7
8
abstract class Arithmetic implements Operand
9
{
10
    const ADD = '+';
11
12
    const SUB = '-';
13
14
    const MUL = '*';
15
16
    const DIV = '/';
17
18
    const MOD = '%';
19
20
    /**
21
     * @var string[]
22
     */
23
    private static $operations = array(
24
        self::ADD,
25
        self::SUB,
26
        self::MUL,
27
        self::DIV,
28
        self::MOD,
29
    );
30
31
    /**
32
     * @var string
33
     */
34
    private $operation = '';
35
36
    /**
37
     * @var Operand|string
38
     */
39
    private $field;
40
41
    /**
42
     * @var Operand|string
43
     */
44
    private $value;
45
46
    /**
47
     * @param string         $operation
48
     * @param Operand|string $field
49
     * @param Operand|string $value
50
     */
51 View Code Duplication
    public function __construct($operation, $field, $value)
0 ignored issues
show
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...
52
    {
53
        if (!in_array($operation, self::$operations)) {
54
            throw new InvalidArgumentException(sprintf(
55
                '"%s" is not a valid arithmetic operation. Valid operations are: "%s"',
56
                $operation,
57
                implode(', ', self::$operations)
58
            ));
59
        }
60
61
        $this->operation = $operation;
62
        $this->field = $field;
63
        $this->value = $value;
64
    }
65
66
    /**
67
     * @param QueryBuilder $qb
68
     * @param string       $dqlAlias
69
     *
70
     * @return string
71
     */
72
    public function transform(QueryBuilder $qb, $dqlAlias)
73
    {
74
        $field = ArgumentToOperandConverter::toField($this->field);
75
        $value = ArgumentToOperandConverter::toValue($this->value);
76
77
        $field = $field->transform($qb, $dqlAlias);
78
        $value = $value->transform($qb, $dqlAlias);
79
80
        return sprintf('(%s %s %s)', $field, $this->operation, $value);
81
    }
82
}
83