Completed
Push — 5.1 ( fe8887...07360b )
by David
25s queued 14s
created

DbalUtils::generateTypes()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 18
rs 9.2222
cc 6
nc 6
nop 1
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Utils;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\ParameterType;
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] = Connection::PARAM_STR_ARRAY;
31
                        continue 2;
32
                    }
33
                }
34
                $types[$key] = Connection::PARAM_INT_ARRAY;
35
            } elseif (is_int($value)) {
36
                $types[$key] = ParameterType::INTEGER;
37
            }
38
        }
39
40
        return $types;
41
    }
42
}
43