AbstractSet::getNotEmptyConditions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Muffin\Conditions\Sets;
4
5
use Muffin\Conditions\AbstractCondition;
6
use Muffin\Conditions\CompositeCondition;
7
use Muffin\Escaper;
8
use Muffin\Condition;
9
10
abstract class AbstractSet extends AbstractCondition implements CompositeCondition
11
{
12
    private
13
    $conditions;
14
15 36
    public function __construct()
16
    {
17 36
        $this->conditions = array();
18 36
    }
19
20 20
    public function toString(Escaper $escaper)
21
    {
22 20
        if($this->isEmpty())
23 20
        {
24 6
            return '';
25
        }
26
27 14
        $condition = $this->buildCompositeCondition();
28
29 14
        return $condition->toString($escaper);
30
    }
31
32 32
    public function add(Condition $condition)
33
    {
34 32
        $this->conditions[] = $condition;
35
36 32
        return $this;
37
    }
38
39 36
    public function isEmpty()
40
    {
41 36
        foreach($this->conditions as $condition)
42
        {
43 32
            if(! $condition->isEmpty())
44 32
            {
45 20
                return false;
46
            }
47 20
        }
48
49 20
        return true;
50
    }
51
52 14
    private function buildCompositeCondition()
53
    {
54 14
        $conditions = $this->getNotEmptyConditions();
55
56 14
        $compositeCondition = array_shift($conditions);
57
58 14
        foreach($conditions as $condition)
59
        {
60 12
            $compositeCondition = $this->joinConditions($compositeCondition, $condition);
61 14
        }
62
63 14
        return $compositeCondition;
64
    }
65
66
    private function getNotEmptyConditions()
67
    {
68 14
        return array_filter($this->conditions, function (Condition $item) {
69 14
            return $item->isEmpty() === false;
70 14
        });
71
    }
72
73
    abstract protected function joinConditions(Condition $leftCondition, Condition $rightCondition);
74
}
75