Passed
Push — master ( bab9dc...bf7b05 )
by Gabriel
02:36
created

Update::assemble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Database\Query;
4
5
/**
6
 * Class Update
7
 * @package Nip\Database\Query
8
 */
9
class Update extends AbstractQuery
10
{
11
12
    /**
13
     * @return string
14
     */
15
    public function assemble()
16
    {
17
        $query = "UPDATE {$this->protect($this->getTable())} SET {$this->parseUpdate()}";
18
19
        $query .= $this->assembleWhere();
20
        $query .= $this->assembleLimit();
21
22
        return $query;
23
    }
24
25
    /**
26
     * @return bool|string
27
     */
28 1
    public function parseUpdate()
29
    {
30 1
        if (!$this->parts['data']) {
31
            return false;
32
        }
33 1
        $fields = [];
34 1
        foreach ($this->parts['data'] as $data) {
35 1
            foreach ($data as $key => $values) {
36 1
                if (!is_array($values)) {
37
                    $values = [$values];
38
                }
39 1
                $value = $values[0];
40 1
                $quote = isset($values[1]) ? $values[1] : null;
41
42 1
                if (!is_numeric($value)) {
43 1
                    if (is_null($quote)) {
44
                        $quote = true;
45
                    }
46 1
                    if ($quote) {
47
                        $value = $this->getManager()->getAdapter()->quote($value);
48
                    }
49
                }
50
51 1
                $fields[] = "{$this->protect($key)} = $value";
52
            }
53
        }
54
55 1
        return implode(", ", $fields);
56
    }
57
}
58