Values::toString()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 8.5806
cc 4
eloc 13
nc 4
nop 0
crap 4
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\Type;
9
use Puzzle\QueryBuilder\Traits\EscaperAware;
10
use Puzzle\QueryBuilder\Traits\TypeGuesser;
11
12
class Values implements Snippet
13
{
14
    use
15
        EscaperAware,
16
        TypeGuesser;
17
18
    private
19
        $values;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $values.

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...
20
21 13
    public function __construct(array $values = null)
22
    {
23 13
        $this->values = [];
24
25 13
        if(! empty($values))
26
        {
27 7
            $this->values($values);
28
        }
29 13
    }
30
31 12
    public function values(array $values): self
32
    {
33 12
        $this->values[] = $values;
34
35 12
        return $this;
36
    }
37
38 13
    public function toString(): string
39
    {
40 13
        if(empty($this->values))
41
        {
42 1
            throw new \RuntimeException('No values to insert');
43
        }
44
45 12
        $columnsNameList = array_filter(array_keys(reset($this->values)));
46
47 12
        $values = [];
48
49 12
        foreach($this->values as $valuesSet)
50
        {
51 12
            if($columnsNameList !== array_keys($valuesSet))
52
            {
53 1
                throw new \RuntimeException('Cannot insert different schema on the same table.');
54
            }
55
56 12
            $values[] = $this->buildValuesSetString($valuesSet);
57
        }
58
59 11
        return sprintf(
60 11
            '%s VALUES %s',
61 11
            $this->wrapWithParenthesis(implode(', ', $columnsNameList)),
62 11
            implode(', ', $values)
63
        );
64
    }
65
66 12
    private function buildValuesSetString(array $values): string
67
    {
68 12
        $valuesSet = [];
69
70 12
        foreach($values as $columnName => $value)
71
        {
72 12
            if(! empty($columnName))
73
            {
74 12
                $type = $this->guessType($columnName, $value);
75
76 12
                $valuesSet[] = $this->formatValue($type, $value);
77
            }
78
        }
79
80 12
        return $this->wrapWithParenthesis(implode(', ', $valuesSet));
81
    }
82
83 12
    private function formatValue(Type $type, $value)
84
    {
85 12
        if(is_null($value))
86
        {
87 1
            return 'NULL';
88
        }
89
90 12
        return $this->escapeValue($type, $value);
91
    }
92
93 12
    private function escapeValue(Type $type, $value)
94
    {
95 12
        $value = $type->format($value);
96
97 12
        if($type->isEscapeRequired())
98
        {
99 11
            $value = $this->escaper->escape($value);
100
        }
101
102 12
        return $value;
103
    }
104
105 12
    private function wrapWithParenthesis(string $value): string
106
    {
107 12
        return sprintf('(%s)', $value);
108
    }
109
}
110