Completed
Push — master ( f8e495...2bdfa1 )
by David
13s queued 12s
created

DbalUtils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generateArrayTypes() 0 16 5
1
<?php
2
3
4
namespace TheCodingMachine\TDBM\Utils;
5
6
use Doctrine\DBAL\Connection;
7
use function is_array;
8
use function is_int;
9
10
/**
11
 * Utility class to ease the use of DBAL types.
12
 */
13
class DbalUtils
14
{
15
    /**
16
     * If a parameter is an array (used in a "IN" statement), we need to tell Doctrine about it.
17
     * @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
18
     * @param array<string, mixed> $parameters
19
     * @return array<string, int>
20
     */
21
    public static function generateArrayTypes(array $parameters): array
22
    {
23
        $types = [];
24
        foreach ($parameters as $key => $value) {
25
            if (is_array($value)) {
26
                foreach ($value as $val) {
27
                    if (!is_int($val)) {
28
                        $types[$key] = Connection::PARAM_STR_ARRAY;
29
                        continue 2;
30
                    }
31
                }
32
                $types[$key] = Connection::PARAM_INT_ARRAY;
33
            }
34
        }
35
36
        return $types;
37
    }
38
}
39