Passed
Push — main ( 4e824a...56b906 )
by William
03:04 queued 01:26
created

QueryHelpers   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 51
rs 10
c 3
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dynamicQueryFilters() 0 20 5
A bind() 0 23 6
1
<?php
2
3
4
namespace Willry\QueryBuilder;
5
6
7
class QueryHelpers
8
{
9
10
11
    public static function bind(\PDOStatement &$stmt, array $params = [])
12
    {
13
        $defaultType = \PDO::PARAM_STR;
14
15
        foreach ($params as $key => $value) {
16
            $type = $defaultType;
17
18
            if (is_int($value)) {
19
                $type = \PDO::PARAM_INT;
20
            }
21
22
            if (is_resource($value)) {
23
                $type = \PDO::PARAM_LOB;
24
            }
25
26
            if (is_bool($value)) {
27
                $type = \PDO::PARAM_BOOL;
28
            }
29
30
            $stmt->bindValue(
31
                is_string($key) ? $key : $key + 1,
32
                $value,
33
                $type
34
            );
35
        }
36
    }
37
38
    public static function dynamicQueryFilters(array &$queryParams, string $queryString, array $bind)
39
    {
40
        $queryParams["filters"][] = $queryString;
41
        $queryParams["binds"] = !empty($queryParams["binds"]) ? array_merge($queryParams["binds"], $bind) : $bind;
42
43
        $queryString = "";
44
45
        if (empty($queryParams["filters"])) return "";
46
47
        foreach ($queryParams["filters"] as $key => $filtro) {
48
            if ($key === 0) {
49
                $queryString .= $filtro;
50
            } else {
51
                $queryString .= " AND {$filtro}";
52
            }
53
        }
54
55
        $queryParams["queryString"] = $queryString;
56
57
        return $queryParams;
58
    }
59
60
}