getConditionOperator()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Muffin\Conditions;
4
5
use Muffin\Escaper;
6
use Muffin\Type;
7
8
abstract class AbstractComparisonOperatorCondition extends AbstractCondition
9
{
10
    protected
11
        $leftOperand,
12
        $rightOperand;
13
14 159
    public function __construct(Type $leftOperand, $rightOperand)
15
    {
16 159
        $this->leftOperand = $leftOperand;
17 159
        $this->rightOperand = $rightOperand;
18 159
    }
19
20 185
    public function toString(Escaper $escaper)
21
    {
22 185
        if($this->isEmpty())
23 185
        {
24 11
            return '';
25
        }
26
27 174
        return sprintf(
28 174
            '%s %s %s',
29 174
            $this->generateFieldOperand($this->leftOperand),
30 174
            $this->getConditionOperator(),
31 174
            $this->generateRightOperand($escaper)
32 174
        );
33
    }
34
35 174
    private function generateFieldOperand(Type $field)
36
    {
37 174
        return $field->getName();
38
    }
39
40 174
    private function generateRightOperand(Escaper $escaper)
41
    {
42 174
        if($this->rightOperand instanceof Type)
43 174
        {
44 36
            return $this->generateFieldOperand($this->rightOperand);
45
        }
46
47 138
        return $this->escapeValue($this->rightOperand, $escaper);
48
    }
49
50 199
    public function isEmpty()
51
    {
52 199
        $columnName = $this->leftOperand->getName();
53
54 199
        return empty($columnName);
55
    }
56
57
    abstract protected function getConditionOperator();
58
59 138
    private function escapeValue($value, Escaper $escaper)
60
    {
61 138
        $value = $this->leftOperand->format($value);
62
63 138
        if($this->leftOperand->isEscapeRequired())
64 138
        {
65 98
            $value = $escaper->escape($value);
66 98
        }
67
68 138
        return $value;
69
    }
70
}
71