Passed
Push — master ( c94b6d...d25cd5 )
by Nikolaos
02:58
created

Update   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 159
ccs 48
cts 76
cp 0.6316
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A column() 0 12 2
A buildColumns() 0 9 2
A from() 0 5 1
A __construct() 0 6 1
A columns() 0 11 3
A getStatement() 0 8 1
A set() 0 11 2
A hasColumns() 0 3 1
A returning() 0 8 1
A reset() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AtlasPHP
12
 *
13
 * @link    https://github.com/atlasphp/Atlas.Query
14
 * @license https://github.com/atlasphp/Atlas.Query/blob/1.x/LICENSE.md
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\DataMapper\Query;
20
21
use Phalcon\DataMapper\Pdo\Connection;
22
23
use function array_merge;
24
use function is_int;
25
26
/**
27
 * Class Update
28
 */
29
class Update extends AbstractConditions
30
{
31
    /**
32
     * Update constructor.
33
     *
34
     * @param Connection $connection
35
     * @param Bind       $bind
36
     */
37 4
    public function __construct(Connection $connection, Bind $bind)
38
    {
39 4
        parent::__construct($connection, $bind);
40
41 4
        $this->store["FROM"]      = "";
42 4
        $this->store["RETURNING"] = [];
43 4
    }
44
45
    /**
46
     * Sets a column for the `UPDATE` query
47
     *
48
     * @param string     $column
49
     * @param mixed|null $value
50
     * @param int        $type
51
     *
52
     * @return Update
53
     */
54 2
    public function column(
55
        string $column,
56
        $value = null,
57
        int $type = -1
58
    ): Update {
59 2
        $this->store["COLUMNS"][$column] = ":" . $column;
60
61 2
        if (null !== $value) {
62 1
            $this->bind->setValue($column, $value, $type);
63
        }
64
65 2
        return $this;
66
    }
67
68
    /**
69
     * Mass sets columns and values for the `UPDATE`
70
     *
71
     * @param array $columns
72
     *
73
     * @return Update
74
     */
75 2
    public function columns(array $columns): Update
76
    {
77 2
        foreach ($columns as $column => $value) {
78 2
            if (is_int($column)) {
79 2
                $this->column($value);
80
            } else {
81 1
                $this->column($column, $value);
82
            }
83
        }
84
85 2
        return $this;
86
    }
87
88
    /**
89
     * Adds table(s) in the query
90
     *
91
     * @param string $table
92
     *
93
     * @return Update
94
     */
95 1
    public function from(string $table): Update
96
    {
97 1
        $this->store["FROM"] = $table;
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 1
    public function getStatement(): string
106
    {
107
        return "UPDATE"
108 1
            . $this->buildFlags()
109 1
            . " " . $this->store["FROM"]
110 1
            . $this->buildColumns()
111 1
            . $this->buildCondition("WHERE")
112 1
            . $this->buildReturning();
113
    }
114
115
    /**
116
     * Whether the query has columns or not
117
     *
118
     * @return bool
119
     */
120 1
    public function hasColumns(): bool
121
    {
122 1
        return count($this->store["COLUMNS"]) > 0;
123
    }
124
125
    /**
126
     * Adds the `RETURNING` clause
127
     *
128
     * @param array $columns
129
     *
130
     * @return Update
131
     */
132 1
    public function returning(array $columns): Update
133
    {
134 1
        $this->store["RETURNING"] = array_merge(
135 1
            $this->store["RETURNING"],
136 1
            $columns
137
        );
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * Resets the internal store
144
     */
145 4
    public function reset(): void
146
    {
147 4
        parent::reset();
148
149 4
        $this->store["FROM"]      = "";
150 4
        $this->store["RETURNING"] = [];
151 4
    }
152
153
    /**
154
     * Sets a column = value condition
155
     *
156
     * @param string     $column
157
     * @param mixed|null $value
158
     *
159
     * @return Update
160
     */
161 1
    public function set(string $column, $value = null): Update
162
    {
163 1
        if (null === $value) {
164 1
            $value = "NULL";
165
        }
166
167 1
        $this->store["COLUMNS"][$column] = $value;
168
169 1
        $this->bind->remove($column);
170
171 1
        return $this;
172
    }
173
174
    /**
175
     * Builds the column list
176
     *
177
     * @return string
178
     */
179 1
    private function buildColumns(): string
180
    {
181 1
        $assignments = [];
182
183 1
        foreach ($this->store["COLUMNS"] as $column => $value) {
184 1
            $assignments[] = $this->quoteIdentifier($column) . " = " . $value;
185
        }
186
187 1
        return " SET" . $this->indent($assignments, ",");
188
    }
189
}
190