Skip to content

Commit 1f06a0f

Browse files
committed
✨ feat: str - add more string helper method and update Str::renderVars logic
1 parent dee1567 commit 1f06a0f

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/Str/StringHelper.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use DateTime;
1313
use Exception;
14+
use RuntimeException;
1415
use Stringable;
1516
use Toolkit\Stdlib\Arr;
1617
use Toolkit\Stdlib\Str\Traits\StringCaseHelperTrait;
@@ -295,25 +296,29 @@ public static function replaces(string $tplCode, array $vars): string
295296
* Simple render vars to template string.
296297
*
297298
* @param string $tplCode
298-
* @param array $vars
299-
* @param string $format Template var format
299+
* @param array $vars
300+
* @param string $format Template var format
301+
* @param bool $mustVar Must find var in $vars, otherwise throw exception
300302
*
301303
* @return string
302304
*/
303-
public static function renderVars(string $tplCode, array $vars, string $format = '{{%s}}'): string
305+
public static function renderVars(string $tplCode, array $vars, string $format = '{{%s}}', bool $mustVar = false): string
304306
{
305307
// get left chars
306-
[$left, $right] = explode('%s', $format);
308+
[$left, $right] = explode('%s', $format, 2);
307309
if (!$vars || !str_contains($tplCode, $left)) {
308310
return $tplCode;
309311
}
310312

311313
$pattern = sprintf('/%s([\w\s.-]+)%s/', preg_quote($left, '/'), preg_quote($right, '/'));
312-
return preg_replace_callback($pattern, static function (array $match) use ($vars) {
314+
return preg_replace_callback($pattern, static function (array $match) use ($vars, $mustVar) {
313315
if ($var = trim($match[1])) {
314-
$val = Arr::getByPath($vars, $var);
315-
if ($val !== null) {
316-
return is_array($val) ? Arr::toStringV2($val) : (string)$val;
316+
$value = Arr::getByPath($vars, $var);
317+
if ($value !== null) {
318+
return is_array($value) ? Arr::toStringV2($value) : (string)$value;
319+
}
320+
if ($mustVar) {
321+
throw new RuntimeException(sprintf('template var not found: %s', $var));
317322
}
318323
}
319324

src/Str/Traits/StringCheckHelperTrait.php

+10
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,16 @@ public static function isVarName(string $string): bool
478478
return preg_match('@^[a-zA-Z_\x7f-\xff][a-zA-Z\d_\x7f-\xff]*$@i', $string) === 1;
479479
}
480480

481+
/**
482+
* @param string $str
483+
*
484+
* @return bool
485+
*/
486+
public static function isAlpha(string $str): bool
487+
{
488+
return preg_match('/^[a-zA-Z]+$/', $str) === 1;
489+
}
490+
481491
/**
482492
* @param string $str
483493
*

test/Str/StringHelperTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ public function testRenderVars(): void
291291

292292
$text = Str::renderVars('tags: ${ tags }', $vars, '${%s}');
293293
$this->assertEquals('tags: [php, java]', $text);
294+
295+
$vars = [
296+
'company' => 'mycompany',
297+
'namePath' => 'group.demo1',
298+
];
299+
$text = Str::renderVars('java/com/{company}/{namePath}', $vars, '{%s}');
300+
$this->assertEquals('java/com/mycompany/group.demo1', $text);
294301
}
295302

296303
public function testBeforeAfter(): void

0 commit comments

Comments
 (0)