summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/PropertyModifier/PropertyModifierRegexReplace.php')
-rw-r--r--library/Director/PropertyModifier/PropertyModifierRegexReplace.php40
1 files changed, 27 insertions, 13 deletions
diff --git a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
index 59cb245..eeae69e 100644
--- a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
+++ b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
@@ -9,25 +9,36 @@ class PropertyModifierRegexReplace extends PropertyModifierHook
{
public static function addSettingsFormFields(QuickForm $form)
{
- $form->addElement('text', 'pattern', array(
- 'label' => 'Regex pattern',
+ $form->addElement('text', 'pattern', [
+ 'label' => $form->translate('Regex pattern'),
'description' => $form->translate(
'The pattern you want to search for. This can be a regular expression like /^www\d+\./'
),
'required' => true,
- ));
+ ]);
- $form->addElement('text', 'replacement', array(
- 'label' => 'Replacement',
+ $form->addElement('text', 'replacement', [
+ 'label' => $form->translate('Replacement'),
'description' => $form->translate(
- 'The string that should be used as a preplacement'
+ 'The string that should be used as a replacement'
),
- ));
+ ]);
+ $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')
+ ]
+ ]);
}
public function getName()
{
- return 'Regular expression based replacement';
+ return mt('director', 'Regular expression based replacement');
}
public function transform($value)
@@ -36,10 +47,13 @@ class PropertyModifierRegexReplace extends PropertyModifierHook
return null;
}
- return preg_replace(
- $this->getSetting('pattern'),
- $this->getSetting('replacement'),
- $value
- );
+ $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;
}
}