summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierMakeBoolean.php
blob: ed91bcff82123bb21acc40816ce9df2243dcb3ea (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

namespace Icinga\Module\Director\PropertyModifier;

use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class PropertyModifierMakeBoolean extends PropertyModifierHook
{
    protected static $validStrings = array(
        '0'     => false,
        'false' => false,
        'n'     => false,
        'no'    => false,
        '1'     => true,
        'true'  => true,
        'y'     => true,
        'yes'   => true,
    );

    public function getName()
    {
        return 'Convert to a boolean value';
    }

    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('select', 'on_invalid', array(
            'label'       => 'Invalid properties',
            'required'    => true,
            'description' => $form->translate(
                'This modifier transforms 0/"0"/false/"false"/"n"/"no" to false'
                . ' and 1, "1", true, "true", "y" and "yes" to true, both in a'
                . ' case insensitive way. What should happen if the given value'
                . ' does not match any of those?'
                . ' You could return a null value, or default to false or true.'
                . ' You might also consider interrupting the whole import process'
                . ' as of invalid source data'
            ),
            'multiOptions' => $form->optionalEnum(array(
                'null'  => $form->translate('Set null'),
                'true'  => $form->translate('Set true'),
                'false' => $form->translate('Set false'),
                'fail'  => $form->translate('Let the import fail'),
            )),
        ));
    }

    public function transform($value)
    {
        if ($value === false || $value === true || $value === null) {
            return $value;
        }

        if ($value === 0) {
            return false;
        }

        if ($value === 1) {
            return true;
        }

        if (is_string($value)) {
            $value = strtolower($value);

            if (array_key_exists($value, self::$validStrings)) {
                return self::$validStrings[$value];
            }
        }

        switch ($this->getSetting('on_invalid')) {
            case 'null':
                return null;

            case 'false':
                return false;

            case 'true':
                return true;

            case 'fail':
            default:
                throw new InvalidPropertyException(
                    '"%s" cannot be converted to a boolean value',
                    $value
                );
        }
    }
}