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

Insert::setCols()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Nip\Database\Query;
5
6
/**
7
 * Class Insert
8
 * @package Nip\Database\Query
9
 */
10
class Insert extends AbstractQuery
11
{
12
    protected $_cols;
13
14
    protected $_values;
15
16
    /**
17
     * @return string
18
     */
19 2
    public function assemble()
20
    {
21 2
        $return = "INSERT INTO " . $this->protect($this->getTable());
22 2
        $return .= $this->parseCols();
23 2
        $return .= $this->parseValues();
24 2
        $return .= $this->parseOnDuplicate();
25
26 2
        return $return;
27
    }
28
29
    /**
30
     * @return string
31
     */
32 2
    public function parseCols()
33
    {
34 2
        if (is_array($this->parts['data'][0])) {
35 2
            $this->setCols(array_keys($this->parts['data'][0]));
36
        }
37 2
        return $this->_cols ? ' (' . implode(',', array_map([$this, 'protect'], $this->_cols)) . ')' : '';
38
    }
39
40
    /**
41
     * @param array|string $cols
42
     * @return $this
43
     */
44 2
    public function setCols($cols = null)
45
    {
46 2
        $this->_cols = $cols;
47 2
        return $this;
48
    }
49
50
    /**
51
     * @return string|false
52
     */
53 2
    public function parseValues()
54
    {
55 2
        if ($this->_values instanceof AbstractQuery) {
56
            return ' ' . (string) $this->_values;
57 2
        } elseif (is_array($this->parts['data'])) {
58 2
            return $this->parseData();
59
        }
60
        return false;
61
    }
62
63
    /**
64
     * Parses INSERT data
65
     *
66
     * @return string
67
     */
68 2
    protected function parseData()
69
    {
70 2
        $values = [];
71 2
        foreach ($this->parts['data'] as $key => $data) {
72 2
            foreach ($data as $value) {
73 2
                if (!is_array($value)) {
74 2
                    $value = [$value];
75
                }
76
77 2
                foreach ($value as $insertValue) {
78 2
                    $values[$key][] = $this->getManager()->getAdapter()->quote($insertValue);
79
                }
80
            }
81
        }
82 2
        foreach ($values as &$value) {
83 2
            $value = "(" . implode(", ", $value) . ")";
84
        }
85
86 2
        return ' VALUES ' . implode(', ', $values);
87
    }
88
89
    /**
90
     * @return string
91
     */
92 2
    public function parseOnDuplicate()
93
    {
94 2
        if ($this->hasPart('onDuplicate')) {
95 1
            $update = $this->getManager()->newUpdate();
96
97 1
            $onDuplicates = $this->getPart('onDuplicate');
98 1
            $data = [];
99 1
            foreach ($onDuplicates as $onDuplicate) {
100 1
                foreach ($onDuplicate as $key => $value) {
101 1
                    $data[$key] = $value;
102
                }
103
            }
104 1
            $update->data($data);
0 ignored issues
show
Unused Code introduced by
The call to Update::data() has too many arguments starting with $data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
105
106 1
            return " ON DUPLICATE KEY UPDATE {$update->parseUpdate()}";
107
        }
108 1
        return '';
109
    }
110
111
    public function setValues($values)
112
    {
113
        $this->_values = $values;
114
115
        return $this;
116
    }
117
118 1
    public function onDuplicate($value)
119
    {
120 1
        $this->addPart('onDuplicate', $value);
121 1
    }
122
}
123