commit d630ee6ba88fc6aec2b66b48985a3ac546c60a1a
parent 938a500f63532accbabfa9fcc6cd624df1c25f2e
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 19 Nov 2020 21:42:39 +0100
Fix: Variables with key '0' are now resolved.
Diffstat:
5 files changed, 59 insertions(+), 14 deletions(-)
diff --git a/dev-helper/test.php b/dev-helper/test.php
@@ -14,6 +14,7 @@ $tests = [
new \util\test\MustacheTest(),
new \security\test\PasswordTest(),
new \util\test\ClassNameTest(),
+ new \util\test\TextMessageTest(),
];
echo '<h1>Running Tests</h1>';
diff --git a/modules/util/test/TextMessageTest.class.php b/modules/util/test/TextMessageTest.class.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace util\test;
+
+use util\text\TextMessage;
+use util\YAML;
+
+class TextMessageTest extends TestCase {
+
+ public function testMessage()
+ {
+ $abc = TextMessage::create('abc ${text}',['text'=>'def']);
+
+ $this->assertEquals('abc \'def\'',$abc);
+ }
+
+ public function testMessageNumberedIndex()
+ {
+ $abc = TextMessage::create('abc ${0}',['def']);
+
+ $this->assertEquals('abc \'def\'',$abc);
+ }
+
+ public function testSanitizer()
+ {
+ $abc = TextMessage::create('abc ${0}',['def/']);
+
+ $this->assertEquals('abc \'def\'(!)',$abc);
+ }
+}
+
+
+
diff --git a/modules/util/text/TextMessage.class.php b/modules/util/text/TextMessage.class.php
@@ -34,7 +34,7 @@ class TextMessage
public static function sanitizeInput( $input ) {
$white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,_-';
$clean = Text::clean($input,$white);
- return "'".$input."'".(strlen($input)>strlen($clean)?'(!)':'');
+ return "'".$clean."'".(strlen($input)>strlen($clean)?'(!)':'');
}
}
\ No newline at end of file
diff --git a/modules/util/text/variables/VariableResolver.class.php b/modules/util/text/variables/VariableResolver.class.php
@@ -36,7 +36,7 @@ class VariableResolver
/**
* Adding a default variable resolver.
- * @param $resolver callable
+ * @param $resolver callable|array
*/
public function addDefaultResolver( $resolver ) {
$this->resolvers[''] = $resolver;
@@ -45,8 +45,8 @@ class VariableResolver
/**
* Adding a variable resolver for a key.
*
- * @param $key key
- * @param $resolver callable
+ * @param $key string key
+ * @param $resolver callable|array
*/
public function addResolver( $key, $resolver ) {
$this->resolvers[$key] = $resolver;
@@ -117,7 +117,10 @@ class VariableResolver
if ( is_callable($resolver) ) {
$v = $resolver( $this->render($expression->name) );
}
- if ( ! $v )
+ elseif ( is_array($resolver) ) {
+ $v = @$resolver[ $this->render($expression->name ) ];
+ }
+ if ( strlen($v)==0 )
$v = $this->render($expression->default);
if ( $this->filterValue ) {
@@ -166,7 +169,7 @@ class VariableResolver
while (true) {
- if ( ! $inputText )
+ if ( strlen($inputText)==0 ) // Do not compare to "false" here, as '0' is false ;)
break;
// Search the next variable marker '$'
diff --git a/modules/util/text/variables/VariablesTest.class.php b/modules/util/text/variables/VariablesTest.class.php
@@ -20,15 +20,23 @@ SRC;
$res->addResolver('me', function($t) {if ($t == 'name') return 'alice';return '';});
$res->addResolver('message', function($t) {return 'this is a message';});
-// echo "Input:\n\n";
-// echo $example."\n\n";
-//
-// echo "Output:\n\n";
-// echo $res->resolveVariables( $example )."\n\n";
-//
-// echo "Resolver:\n\n";
- //print_r($res);
$this->assertNotEmpty($res->resolveVariables( $example ) );
}
+
+ public function testSpecials() {
+
+ $resolver = new VariableResolver();
+ $resolver->addDefaultResolver( ['0','1','2',''=>'space','name'=>'name'] );
+
+ $resolver->marker = '';
+
+ $resolver->parseString('a{0}b');
+
+ $this->assertEquals( 'name',$resolver->resolveVariables('{name}') );
+ $this->assertEquals( 'space',$resolver->resolveVariables('{}') );
+ $this->assertEquals( '2',$resolver->resolveVariables('{2}') );
+ $this->assertEquals( '1',$resolver->resolveVariables('{1}') );
+ $this->assertEquals( '0',$resolver->resolveVariables('{0}') );
+ }
}