Passed
Push — master ( 4664b1...ce77b3 )
by
unknown
03:32
created

DbalUtils::generateTypes()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 7
nop 1
dl 0
loc 20
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\ArrayParameterType;
6
use Doctrine\DBAL\ParameterType;
7
8
use function is_array;
9
use function is_int;
10
11
/**
12
 * Utility class to ease the use of DBAL types.
13
 */
14
class DbalUtils
15
{
16
    /**
17
     * If a parameter is an array (used in a "IN" statement), we need to tell Doctrine about it.
18
     * If it is an integer we have to tell DBAL (default is string)
19
     * @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
20
     * @param array<string, mixed> $parameters
21
     * @return array<string, int>
22
     */
23
    public static function generateTypes(array $parameters): array
24
    {
25
        $types = [];
26
        foreach ($parameters as $key => $value) {
27
            if (is_array($value)) {
28
                foreach ($value as $val) {
29
                    if (!is_int($val)) {
30
                        $types[$key] = ArrayParameterType::STRING;
31
                        continue 2;
32
                    }
33
                }
34
                $types[$key] = ArrayParameterType::INTEGER;
35
            } elseif (is_int($value)) {
36
                $types[$key] = ParameterType::INTEGER;
37
            } elseif (is_bool($value)) {
38
                $types[$key] = ParameterType::BOOLEAN;
39
            }
40
        }
41
42
        return $types;
43
    }
44
}
45