summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/Db/DbDataFormatter.php
blob: 91fc776130cb3157cc23141ea58cf35f91c71d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace Icinga\Module\Director\Data\Db;

use InvalidArgumentException;

class DbDataFormatter
{
    public static function normalizeBoolean($value): ?string
    {
        if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
            return 'y';
        }
        if ($value === 'n' || $value === '0' || $value === false || $value === 0) {
            return 'n';
        }
        if ($value === '' || $value === null) {
            return null;
        }

        throw new InvalidArgumentException(sprintf(
            'Got invalid boolean: %s',
            var_export($value, true)
        ));
    }

    public static function booleanForDbValue($value): ?bool
    {
        if ($value === 'y') {
            return true;
        }
        if ($value === 'n') {
            return false;
        }

        return $value; // let this fail elsewhere, if not null
    }
}