Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit caf723d

Browse files
committed
up: str - refactor the Str.renderTemplate logic
1 parent 8a3b0ed commit caf723d

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

‎src/Str/StringHelper.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use DateTime;
1313
use Exception;
1414
use Stringable;
15+
use Toolkit\Stdlib\Arr;
1516
use Toolkit\Stdlib\Str\Traits\StringCaseHelperTrait;
1617
use Toolkit\Stdlib\Str\Traits\StringCheckHelperTrait;
1718
use Toolkit\Stdlib\Str\Traits\StringConvertTrait;
@@ -33,6 +34,8 @@
3334
use function mb_strwidth;
3435
use function microtime;
3536
use function preg_match;
37+
use function preg_quote;
38+
use function preg_replace_callback;
3639
use function preg_split;
3740
use function random_bytes;
3841
use function random_int;
@@ -46,6 +49,7 @@
4649
use function strlen;
4750
use function strtr;
4851
use function substr;
52+
use function trim;
4953
use function uniqid;
5054
use const STR_PAD_LEFT;
5155
use const STR_PAD_RIGHT;
@@ -279,7 +283,7 @@ public static function genNOV2(string|int|Stringable $prefix = '', array $random
279283

280284
/**
281285
* @param string $tplCode
282-
* @param array $vars
286+
* @param array $vars [k => v]
283287
*
284288
* @return string
285289
*/
@@ -289,28 +293,47 @@ public static function replaces(string $tplCode, array $vars): string
289293
}
290294

291295
/**
296+
* Simple render vars to template string.
297+
*
292298
* @param string $tplCode
293299
* @param array $vars
294-
* @param string $format
300+
* @param string $format Template var format
295301
*
296302
* @return string
297303
*/
298-
public static function renderTemplate(string $tplCode, array $vars, string $format = '{{%s}}'): string
304+
public static function renderVars(string $tplCode, array $vars, string $format = '{{%s}}'): string
299305
{
300306
// get left chars
301-
[$left, ] = explode('%s', $format);
307+
[$left, $right] = explode('%s', $format);
302308
if (!$vars || !str_contains($tplCode, $left)) {
303309
return $tplCode;
304310
}
305311

306-
$fmtVars = [];
307-
foreach ($vars as $name => $var) {
308-
$name = sprintf($format, (string)$name);
309-
// add
310-
$fmtVars[$name] = $var;
311-
}
312+
$fmtVars = Arr::flattenMap($vars, Arr::FLAT_DOT_JOIN_INDEX);
313+
$pattern = sprintf('/%s([\w\s.-]+)%s/', preg_quote($left, '/'), preg_quote($right, '/'));
314+
315+
return preg_replace_callback($pattern, static function (array $match) use ($fmtVars) {
316+
$var = trim($match[1]);
317+
if ($var && isset($fmtVars[$var])) {
318+
return $fmtVars[$var];
319+
}
312320

313-
return strtr($tplCode, $fmtVars);
321+
return $match[0];
322+
}, $tplCode);
323+
}
324+
325+
/**
326+
* Alias of renderVars().
327+
*
328+
* @param string $tplCode
329+
* @param array $vars
330+
* @param string $format Template var format
331+
*
332+
* @return string
333+
*/
334+
public static function renderTemplate(string $tplCode, array $vars, string $format = '{{%s}}'): string
335+
{
336+
return self::renderVars($tplCode, $vars, $format);
314337
}
315338

316339
////////////////////////////////////////////////////////////

‎test/Str/StringHelperTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,22 @@ public function testSplitTypedList(): void
267267
$this->assertEquals($want, Str::toTypedArray($given));
268268
}
269269
}
270+
271+
public function testRenderVars(): void
272+
{
273+
$vars = [
274+
'name' => 'inhere',
275+
'age' => 200,
276+
'tags' => ['php'],
277+
'top' => [
278+
'key0' => 'val0',
279+
]
280+
];
281+
282+
$text = Str::renderVars('hello {{ name }}, age: {{ age}}, tags: {{ tags.0 }}, key0: {{top.key0 }}', $vars);
283+
$this->assertEquals('hello inhere, age: 200, tags: php, key0: val0', $text);
284+
285+
$text = Str::renderVars('hello ${ name }, age: ${ age}, tags: ${ tags.0 }, key0: ${top.key0 }', $vars, '${%s}');
286+
$this->assertEquals('hello inhere, age: 200, tags: php, key0: val0', $text);
287+
}
270288
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /