Completed
Pull Request — master (#3)
by Rémy
08:33
created

Helper::isMySQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author Rémy M. Böhler <[email protected]>
4
 */
5
declare(strict_types=1);
6
7
namespace Rorm;
8
9
class Helper
10
{
11
    public function isMySQL(\PDO $dbh): bool
12
    {
13
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql';
14
    }
15
16
    public function isSQLite(\PDO $dbh): bool
17
    {
18
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite';
19
    }
20
21
    public function quote(\PDO $dbh, $value)
22
    {
23
        switch (true) {
24
            case is_bool($value):
25
                if ($this->isMySQL($dbh)) {
26
                    return $value ? 'TRUE' : 'FALSE';
27
                }
28
                return $value ? 1 : 0;
29
            case $value === null:
30
                return 'NULL';
31
            case is_int($value):
32
                return (int)$value;
33
            case is_float($value):
34
                return (float)$value;
35
            default:
36
                return $dbh->quote($value);
37
        }
38
    }
39
40
    /**
41
     * Method to quote identifiers
42
     * Please make sure you keep the quoter as long you are needing it.
43
     */
44
    public function getIdentifierQuoter(\PDO $dbh): callable
45
    {
46
        if ($this->isMySQL($dbh)) {
47
            // mysql mode
48
            return function ($identifier) {
49
                return '`' . str_replace('`', '``', $identifier) . '`';
50
            };
51
        }
52
53
        // standard sql mode
54
        return function ($identifier) {
55
            return '"' . str_replace('"', '""', $identifier) . '"';
56
        };
57
    }
58
}
59