Completed
Push — 1.0 ( 8e07ac...ea6409 )
by Peter
08:50
created

Arithmetic::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
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...
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