AbstractInCondition::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Muffin\Conditions;
4
5
use Muffin\Escaper;
6
use Muffin\Type;
7
8
abstract class AbstractInCondition extends AbstractCondition
9
{
10
    protected
11
        $type,
12
        $values;
13
14 25
    public function __construct(Type $column, array $values)
15
    {
16 25
        $this->type = $column;
17 25
        $this->values = $values;
18 25
    }
19
20 18
    public function toString(Escaper $escaper)
21
    {
22 18
        if($this->isEmpty())
23 18
        {
24 2
            return '';
25
        }
26
27 16
        $values = $this->escapeValues($this->values, $escaper);
28
29 16
        return sprintf(
30 16
            '%s %s (%s)',
31 16
            $this->type->getName(),
32 16
            $this->getOperator(),
33 16
            implode(', ', $values)
34 16
        );
35
    }
36
37 25
    public function isEmpty()
38
    {
39 25
        $columnName = $this->type->getName();
40
41 25
        return empty($columnName);
42
    }
43
44
    abstract protected function getOperator();
45
46 16
    protected function escapeValues(array $values, Escaper $escaper)
47
    {
48 16
        $escapedValues = array();
49
50 16
        foreach($values as $value)
51
        {
52 16
            $formatedValue = $this->type->format($value);
53 16
            if($this->type->isEscapeRequired())
54 16
            {
55 12
                $formatedValue = $escaper->escape($formatedValue);
56 12
            }
57
58 16
            $escapedValues[] = $formatedValue;
59 16
        }
60
61 16
        return $escapedValues;
62
    }
63
}
64