mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-29 08:44:11 +02:00
parent
e58c31ab63
commit
2ff722e4d2
@ -0,0 +1,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_missing')) {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
1
run.php
1
run.php
@ -32,6 +32,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
|
||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromAdSid');
|
||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromLatin1');
|
||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierBitmask');
|
||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierMakeBoolean');
|
||||
|
||||
$this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
|
||||
$this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');
|
||||
|
Loading…
x
Reference in New Issue
Block a user