Skip to content

Commit

Permalink
feat: Allow whitespace in template vars
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Mar 16, 2021
1 parent 05605a2 commit 18297e7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
47 changes: 3 additions & 44 deletions src/CustomForm.php
Expand Up @@ -124,20 +124,7 @@ function CustomForm()
}

// Add this fields value to the list of variables for the output post.
$vars[] = '/\{' . $field['title'] . '\}/';
$replace[] = str_replace('$', '\$', $value);

// {{ }} Syntax: Setup REGEX for removing entire {{ }} string or just stripping the outermost { }, depending upon the replacement value being blank or not
if ($value == '')
{
$vars_blank[] = '/\{[^\{\}]*\{' . $field['title'] . '\}[^\{\}]*\}/';
$vars_non_blank[] = '//';
}
else
{
$vars_blank[] = '//';
$vars_non_blank[] = '/\{[^\{\}]*\{' . $field['title'] . '\}[^\{\}]*\}/';
}
$vars[$field['title']] = $value;
}

// Check whether the visual verification code was entered correctly.
Expand All @@ -163,39 +150,11 @@ function CustomForm()
if (!$fail_submit)
{
require_once($sourcedir . '/Subs-Post.php');

// {{ }} Syntax: Strip out everything in {{ }} if value is blank
$output = preg_replace($vars_blank, '', $output);
$subject = preg_replace($vars_blank, '', $subject);

// {{ }} Syntax: Remove outside brackets if value is not blank
$output = preg_replace_callback(
$vars_non_blank,
function ($matches)
{
return substr($matches[0],1,-1);
},
$output
);
$subject = preg_replace_callback(
$vars_non_blank,
function ($matches)
{
return substr($matches[0],1,-1);
},
$subject
);

// Replace all vars with their correct value, for both the message and the subject.
$output = preg_replace($vars, $replace, $output);
$subject = preg_replace($vars, $replace, $subject);

// Collect all necessary parameters for the creation of the post.
$msgOptions = array(
'id' => 0,
'subject' => $subject,
'subject' => customform_replace_vars($subject, $vars),
'icon' => $icon,
'body' => $output,
'body' => customform_replace_vars($output, $vars),
'smileys_enabled' => true,
);

Expand Down
10 changes: 10 additions & 0 deletions src/Subs-CustomForm.php
Expand Up @@ -33,4 +33,14 @@ function customform_list_classes()
foreach (get_declared_classes() as $class)
if (is_subclass_of($class, 'CustomFormBase'))
yield trim(strpbrk($class, '_'), '_');
}

function customform_replace_vars(string $text, array $array): string
{
return preg_replace_callback('~{{1,2}\s*?([a-zA-Z0-9\-_\.]+)\s*?}{1,2}~',
function($matches) use ($array)
{
return $array[$matches[1]] ?? $matches[1];
},
$text);
}
43 changes: 43 additions & 0 deletions tests/Test.php
Expand Up @@ -6,6 +6,49 @@

final class Test extends TestCase
{
/**
* @dataProvider replaceProvider
*/
public function testReplace(string $test, string $expected): void
{
$result = customform_replace_vars($test, []);
$this->assertSame($expected, $result);
}

/**
* @return string[][]
*/
public function replaceProvider(): array
{
return [
['abc{{def}}ghi', 'abcdefghi'],
['abc{{def }}ghi', 'abcdefghi'],
['abc{{def }}ghi', 'abcdefghi'],
['abc{{ def}}ghi', 'abcdefghi'],
['abc{{ def}}ghi', 'abcdefghi'],
['abc{{ def }}ghi', 'abcdefghi'],
['abc{{ def }}ghi', 'abcdefghi'],
['abc{def}ghi', 'abcdefghi'],
['abc{def }ghi', 'abcdefghi'],
['abc{def }ghi', 'abcdefghi'],
['abc{ def}ghi', 'abcdefghi'],
['abc{ def}ghi', 'abcdefghi'],
['abc{ def }ghi', 'abcdefghi'],
['abc{ def }ghi', 'abcdefghi']
];
}

public function testReplaceVar(): void
{
$this->assertSame(
'Hello world',
customform_replace_vars(
'Hello {{ term }}',
['term' => 'world']
)
);
}

public function testFindClasses(): void
{
$classes = iterator_to_array(customform_list_classes());
Expand Down

0 comments on commit 18297e7

Please sign in to comment.