Passed
Push — master ( 2295a4...eec722 )
by Peter
10:03
created

Mysql::nullableParam()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Helper;
6
7
class Mysql
8
{
9
    public const OPTION_EMPTY       = 0;
10
    public const OPTION_PREFER_DATE = 1;
11
12
    /**
13
     * @param     $value
14
     * @param int $type
15
     * @param int $option
16
     *
17
     * @return string[]
18
     */
19
    public static function nullableParam($value, int $type = \PDO::PARAM_STR, int $option = self::OPTION_EMPTY): array
20
    {
21
        if ($value === null) {
22
            return [null, \PDO::PARAM_NULL];
23
        }
24
25
        if ($value instanceof \DateTime) {
26
            if ($option & static::OPTION_PREFER_DATE) {
27
                return [DateHelper::mysqlDate($value), $type];
28
            }
29
30
            return [DateHelper::mysqlDateTime($value), $type];
31
        }
32
33
        return [$value, $type];
34
    }
35
}
36