Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Set
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderSet
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 renderSetCheckingConflicts
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 hasSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Database Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Database\Manipulation\Traits;
11
12use Closure;
13use LogicException;
14
15/**
16 * Trait Set.
17 *
18 * @package database
19 */
20trait Set
21{
22    /**
23     * Sets the SET clause.
24     *
25     * @param array<string,Closure|float|int|string|null>|object $columns Array
26     * of columns => values or an object to be cast to array
27     *
28     * @return static
29     */
30    public function set(array | object $columns) : static
31    {
32        $this->sql['set'] = (array) $columns;
33        return $this;
34    }
35
36    /**
37     * Renders the SET clause.
38     *
39     * @return string|null The SET clause null if it was not set
40     */
41    protected function renderSet() : ?string
42    {
43        if ( ! $this->hasSet()) {
44            return null;
45        }
46        $set = [];
47        foreach ($this->sql['set'] as $column => $value) {
48            $set[] = $this->renderAssignment($column, $value);
49        }
50        $set = \implode(', ', $set);
51        return " SET {$set}";
52    }
53
54    /**
55     * Renders the SET clause checking conflicts.
56     *
57     * @throws LogicException if SET was set with columns or with the VALUES clause
58     *
59     * @return string|null The SET part or null if it was not set
60     */
61    protected function renderSetCheckingConflicts() : ?string
62    {
63        $part = $this->renderSet();
64        if ($part === null) {
65            return null;
66        }
67        if (isset($this->sql['columns'])) {
68            throw new LogicException('SET clause is not allowed when columns are set');
69        }
70        if (isset($this->sql['values'])) {
71            throw new LogicException('SET clause is not allowed when VALUES is set');
72        }
73        return $part;
74    }
75
76    /**
77     * Tells if the SET clause was set.
78     *
79     * @return bool True if was set, otherwise false
80     */
81    protected function hasSet() : bool
82    {
83        return isset($this->sql['set']);
84    }
85}