File modules/util/text/TextMessage.class.php

Last commit: Thu Nov 19 21:42:39 2020 +0100	Jan Dankert	Fix: Variables with key '0' are now resolved.
1 <?php 2 3 4 namespace util\text; 5 6 7 use util\Text; 8 use util\text\variables\VariableResolver; 9 10 class TextMessage 11 { 12 /** 13 * Creates a text message with variables. 14 * 15 * @param $text 16 * @param array $params 17 * @return string 18 */ 19 public static function create( $text, $params=[] ) { 20 if ( $params) { 21 $resolver = new VariableResolver(); 22 $resolver->addDefaultResolver( function($key) use ($params) { 23 return TextMessage::sanitizeInput( @$params[$key] ); 24 }); 25 return $resolver->resolveVariables($text); 26 } 27 else { 28 // no params, so no resolver needed 29 return $text; 30 } 31 } 32 33 34 public static function sanitizeInput( $input ) { 35 $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,_-'; 36 $clean = Text::clean($input,$white); 37 return "'".$clean."'".(strlen($input)>strlen($clean)?'(!)':''); 38 } 39 40 }
Download modules/util/text/TextMessage.class.php
History Thu, 19 Nov 2020 21:42:39 +0100 Jan Dankert Fix: Variables with key '0' are now resolved. Mon, 26 Oct 2020 22:21:42 +0100 Jan Dankert Refactoring: Using TextMessage for creating Messages with user content.