Passed
Branch test (5f2a6d)
by William
02:24
created

QueryHelpers::applyDefaultFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
4
namespace Willry\QueryBuilder;
5
6
7
class QueryHelpers
8
{
9
10
    public static function applyDefaultFilter(array $data): ?array
11
    {
12
        $filter = [];
13
        foreach ($data as $key => $value) {
14
            $filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT));
15
        }
16
        return $filter;
17
    }
18
19
    public static function bind(\PDOStatement &$stmt, array $params = [])
20
    {
21
        $binds = self::applyDefaultFilter($params);
22
23
        foreach ($binds as $key => $bind) {
24
            if ($key == 'limit' || $key == "offset") {
25
                $stmt->bindValue(":$key", $bind, \PDO::PARAM_INT);
26
            } else {
27
                $stmt->bindValue(":$key", $bind);
28
            }
29
        }
30
31
        return $binds;
32
    }
33
34
    public static function dynamicQueryFilters(array &$queryParams, string $queryString, array $bind)
35
    {
36
        $queryParams["filters"][] = $queryString;
37
        $queryParams["binds"] = !empty($queryParams["binds"]) ? array_merge($queryParams["binds"], $bind) : $bind;
38
39
        $queryString = "";
40
41
        if (empty($queryParams["filters"])) return "";
42
43
        foreach ($queryParams["filters"] as $key => $filtro) {
44
            if ($key === 0) {
45
                $queryString .= $filtro;
46
            } else {
47
                $queryString .= " AND {$filtro}";
48
            }
49
        }
50
51
        $queryParams["queryString"] = $queryString;
52
53
        return $queryParams;
54
    }
55
56
}