Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Values | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
values | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
renderValues | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 |
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 | */ |
10 | namespace Framework\Database\Manipulation\Traits; |
11 | |
12 | use Closure; |
13 | use LogicException; |
14 | |
15 | /** |
16 | * Trait Values. |
17 | * |
18 | * @package database |
19 | */ |
20 | trait Values |
21 | { |
22 | /** |
23 | * Adds a row of values to the VALUES clause. |
24 | * |
25 | * @param array<array<mixed>>|Closure|float|int|string|null $value |
26 | * @param Closure|float|int|string|null ...$values |
27 | * |
28 | * @return static |
29 | */ |
30 | public function values( |
31 | array | Closure | float | int | string | null $value, |
32 | Closure | float | int | string | null ...$values |
33 | ) : static { |
34 | if ( ! \is_array($value)) { |
35 | $this->sql['values'][] = [$value, ...$values]; |
36 | return $this; |
37 | } |
38 | if ($values) { |
39 | throw new LogicException( |
40 | 'The method ' . static::class . '::values' |
41 | . ' must have only one argument when the first parameter is passed as array' |
42 | ); |
43 | } |
44 | foreach ($value as $row) { |
45 | $this->sql['values'][] = $row; |
46 | } |
47 | return $this; |
48 | } |
49 | |
50 | /** |
51 | * Renders the VALUES clause. |
52 | * |
53 | * @return string|null The VALUES part or null if none was set |
54 | */ |
55 | protected function renderValues() : ?string |
56 | { |
57 | if ( ! isset($this->sql['values'])) { |
58 | return null; |
59 | } |
60 | $values = []; |
61 | foreach ($this->sql['values'] as $value) { |
62 | foreach ($value as &$item) { |
63 | $item = $this->renderValue($item); |
64 | } |
65 | unset($item); |
66 | $values[] = ' (' . \implode(', ', $value) . ')'; |
67 | } |
68 | $values = \implode(',' . \PHP_EOL, $values); |
69 | return " VALUES{$values}"; |
70 | } |
71 | } |