Where::toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries\Snippets;
6
7
use Puzzle\QueryBuilder\Snippet;
8
use Puzzle\QueryBuilder\Condition;
9
use Puzzle\QueryBuilder\Traits\EscaperAware;
10
11
class Where implements Snippet
12
{
13
    use EscaperAware;
14
15
    private
16
        $conditions;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $conditions.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18 40
    public function __construct(Condition $condition = null)
19
    {
20 40
        $this->conditions = array();
21
22 40
        if($condition instanceof Condition)
23
        {
24 4
            $this->addCondition($condition);
25
        }
26 40
    }
27
28 24
    public function where(Condition $condition): self
29
    {
30 24
        $this->addCondition($condition);
31
32 24
        return $this;
33
    }
34
35 34
    public function toString(): string
36
    {
37 34
        $conditionString = $this->buildConditionString();
38 34
        if(empty($conditionString))
39
        {
40 8
            return '';
41
        }
42
43 27
        return sprintf('WHERE %s', $conditionString);
44
    }
45
46 34
    private function buildConditionString(): string
47
    {
48 34
        $whereConditions = array();
49 34
        foreach($this->conditions as $condition)
50
        {
51 28
            $conditionString = $condition->toString($this->escaper);
52 28
            if(! empty($conditionString))
53
            {
54 27
                $whereConditions[] = $conditionString;
55
            }
56
        }
57
58 34
        return implode(' AND ', $whereConditions);
59
    }
60
61 28
    private function addCondition(Condition $condition): void
62
    {
63 28
        $this->conditions[] = $condition;
64 28
    }
65
}
66