2015-07-23 14:29:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\PropertyModifier;
|
|
|
|
|
2016-02-17 11:11:05 +01:00
|
|
|
use Icinga\Module\Director\Hook\PropertyModifierHook;
|
2016-02-18 23:21:28 +01:00
|
|
|
use Icinga\Module\Director\Web\Form\QuickForm;
|
2015-07-23 14:29:15 +02:00
|
|
|
|
|
|
|
class PropertyModifierRegexReplace extends PropertyModifierHook
|
|
|
|
{
|
2015-07-23 14:42:53 +02:00
|
|
|
public static function addSettingsFormFields(QuickForm $form)
|
|
|
|
{
|
2023-02-23 11:44:13 +01:00
|
|
|
$form->addElement('text', 'pattern', [
|
|
|
|
'label' => $form->translate('Regex pattern'),
|
2016-03-06 00:37:03 +01:00
|
|
|
'description' => $form->translate(
|
|
|
|
'The pattern you want to search for. This can be a regular expression like /^www\d+\./'
|
|
|
|
),
|
|
|
|
'required' => true,
|
2023-02-23 11:44:13 +01:00
|
|
|
]);
|
2016-02-18 23:21:28 +01:00
|
|
|
|
2023-02-23 11:44:13 +01:00
|
|
|
$form->addElement('text', 'replacement', [
|
|
|
|
'label' => $form->translate('Replacement'),
|
2016-03-06 00:37:03 +01:00
|
|
|
'description' => $form->translate(
|
2023-02-23 11:44:13 +01:00
|
|
|
'The string that should be used as a replacement'
|
2016-03-06 00:37:03 +01:00
|
|
|
),
|
2023-02-23 11:44:13 +01:00
|
|
|
]);
|
|
|
|
$form->addElement('select', 'when_not_matched', [
|
|
|
|
'label' => $form->translate('When not matched'),
|
|
|
|
'description' => $form->translate(
|
|
|
|
"What should happen, if the given pattern doesn't match"
|
|
|
|
),
|
|
|
|
'value' => 'keep',
|
|
|
|
'multiOptions' => [
|
|
|
|
'keep' => $form->translate('Keep the given string'),
|
|
|
|
'set_null' => $form->translate('Set the value to NULL')
|
|
|
|
]
|
|
|
|
]);
|
2015-07-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 00:37:03 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
2023-02-23 11:44:13 +01:00
|
|
|
return mt('director', 'Regular expression based replacement');
|
2016-03-06 00:37:03 +01:00
|
|
|
}
|
|
|
|
|
2015-07-23 14:29:15 +02:00
|
|
|
public function transform($value)
|
|
|
|
{
|
2021-08-12 11:45:40 +02:00
|
|
|
if ($value === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-23 11:44:13 +01:00
|
|
|
$result = preg_replace($this->getSetting('pattern'), $this->getSetting('replacement'), $value);
|
|
|
|
if ($result === $value && $this->getSetting('when_not_matched', 'keep') === 'set_null') {
|
|
|
|
if (!preg_match($this->getSetting('pattern'), $value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2015-07-23 14:29:15 +02:00
|
|
|
}
|
|
|
|
}
|