WhereToken::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Hgraca\MicroDbal\Crud\QueryBuilder\Sql;
4
5
final class WhereToken
6
{
7
    const BINDING_PREFIX = ':w';
8
9
    const COMPARISON_LESS = '<';
10
    const COMPARISON_LESS_OR_EQUAL = '<=';
11
    const COMPARISON_EQUAL = '=';
12
    const COMPARISON_HIGHER_OR_EQUAL = '>=';
13
    const COMPARISON_HIGHER = '>';
14
    const COMPARISON_IS = ' IS ';
15
16
    /**
17
     * @var string
18
     */
19
    private $column;
20
21
    /**
22
     * @var mixed
23
     */
24
    private $value;
25
26
    /**
27
     * @var int
28
     */
29
    private $bindingNumber;
30
31
    /**
32
     * @var string
33
     */
34
    private $comparison;
35
36 22
    public function __construct(
37
        string $column,
38
        $value,
39
        int $bindingCounter = 0,
40
        string $comparison = self::COMPARISON_EQUAL
41
    ) {
42 22
        $this->column = $column;
43 22
        $this->value = $value;
44 22
        $this->bindingNumber = $bindingCounter;
45 22
        $this->comparison = ($value === null) ? self::COMPARISON_IS : $comparison;
46 22
    }
47
48 19
    public function toString(): string
49
    {
50 19
        return ($this->value instanceof WhereTokenCollection)
51 7
            ? '(' . $this->value->toString() . ')'
52 19
            : '`' . $this->column . '`' . $this->comparison . self::BINDING_PREFIX . $this->bindingNumber;
53
    }
54
55 13
    public function getBinding(): array
56
    {
57 13
        return ($this->value instanceof WhereTokenCollection)
58 5
            ? $this->value->getBindingsList()
59 13
            : [self::BINDING_PREFIX . $this->bindingNumber => $this->value];
60
    }
61
}
62