Package system sms

This commit is contained in:
Simon Pocrnjič
2025-10-26 12:57:09 +01:00
parent 266af6595e
commit 369af34ad4
29 changed files with 2639 additions and 330 deletions
+41 -5
View File
@@ -79,16 +79,52 @@ public function sendFromTemplate(SmsTemplate $template, string $to, array $varia
return $log;
}
protected function renderContent(string $content, array $vars): string
public function renderContent(string $content, array $vars): string
{
// Simple token replacement: {token}
return preg_replace_callback('/\{([a-zA-Z0-9_\.]+)\}/', function ($m) use ($vars) {
$key = $m[1];
// Support {token} and {nested.keys} using dot-notation lookup
$resolver = function (array $arr, string $path) {
if (array_key_exists($path, $arr)) {
return $arr[$path];
}
$segments = explode('.', $path);
$cur = $arr;
foreach ($segments as $seg) {
if (is_array($cur) && array_key_exists($seg, $cur)) {
$cur = $cur[$seg];
} else {
return null;
}
}
return array_key_exists($key, $vars) ? (string) $vars[$key] : $m[0];
return $cur;
};
return preg_replace_callback('/\{([a-zA-Z0-9_\.]+)\}/', function ($m) use ($vars, $resolver) {
$key = $m[1];
$val = $resolver($vars, $key);
return $val !== null ? (string) $val : $m[0];
}, $content);
}
/**
* Format a number to EU style: thousands separated by '.', decimals by ','.
*/
public function formatAmountEu(mixed $value, int $decimals = 2): string
{
if ($value === null || $value === '') {
return number_format(0, $decimals, ',', '.');
}
$str = (string) $value;
// Normalize possible EU-style input like "1.234,56" to standard for float casting
if (str_contains($str, ',')) {
$str = str_replace(['.', ','], ['', '.'], $str);
}
$num = (float) $str;
return number_format($num, $decimals, ',', '.');
}
/**
* Get current credit balance from provider.
*/