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 20b2fa3

Browse files
committed
up: enhance the Str.renderVars, support print array as string
1 parent 0f865a4 commit 20b2fa3

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

‎src/Any.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib;
4+
5+
use Toolkit\Stdlib\Helper\DataHelper;
6+
7+
/**
8+
* class Any
9+
*
10+
* @author inhere
11+
* @date 2022年12月30日
12+
*/
13+
final class Any extends DataHelper
14+
{
15+
16+
}

‎src/Str/StringHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function gethostname;
3131
use function hash;
3232
use function hex2bin;
33+
use function is_array;
3334
use function is_int;
3435
use function mb_strwidth;
3536
use function microtime;
@@ -309,9 +310,16 @@ public static function renderVars(string $tplCode, array $vars, string $format =
309310
return $tplCode;
310311
}
311312

312-
$fmtVars = Arr::flattenMap($vars, Arr::FLAT_DOT_JOIN_INDEX);
313+
$fmtVars = Arr::flattenMap($vars, Arr\ArrConst::FLAT_DOT_JOIN_INDEX);
313314
$pattern = sprintf('/%s([\w\s.-]+)%s/', preg_quote($left, '/'), preg_quote($right, '/'));
314315

316+
// convert array value to string.
317+
foreach ($vars as $name => $val) {
318+
if (is_array($val)) {
319+
$fmtVars[$name] = Arr::toStringV2($val);
320+
}
321+
}
322+
315323
return preg_replace_callback($pattern, static function (array $match) use ($fmtVars) {
316324
$var = trim($match[1]);
317325
if ($var && isset($fmtVars[$var])) {

‎src/Str/Traits/StringCheckHelperTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,17 @@ public static function isAlphaNum(string $str): bool
489489
return preg_match('/^[a-zA-Z0-9]+$/', $str) === 1;
490490
}
491491

492+
/**
493+
* @param string $pattern
494+
* @param string $str
495+
*
496+
* @return bool
497+
*/
498+
public static function pregMatch(string $pattern, string $str): bool
499+
{
500+
return preg_match($pattern, $str) === 1;
501+
}
502+
492503
/**
493504
* 使用正则验证数据
494505
*

‎test/Str/StringHelperTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function testBasicStrMethods(): void
2727

2828
$this->assertTrue(Str::isNull('null'));
2929
$this->assertFalse(Str::isNull('abc'));
30+
31+
$this->assertTrue(Str::isAlphaNum('abc'));
32+
$this->assertTrue(Str::isAlphaNum('abc23'));
33+
$this->assertFalse(Str::isAlphaNum('--'));
3034
}
3135

3236
public function testIsBool(): void
@@ -273,7 +277,7 @@ public function testRenderVars(): void
273277
$vars = [
274278
'name' => 'inhere',
275279
'age' => 200,
276-
'tags' => ['php'],
280+
'tags' => ['php', 'java'],
277281
'top' => [
278282
'key0' => 'val0',
279283
]
@@ -284,5 +288,8 @@ public function testRenderVars(): void
284288

285289
$text = Str::renderVars('hello ${ name }, age: ${ age}, tags: ${ tags.0 }, key0: ${top.key0 }', $vars, '${%s}');
286290
$this->assertEquals('hello inhere, age: 200, tags: php, key0: val0', $text);
291+
292+
$text = Str::renderVars('tags: ${ tags }', $vars, '${%s}');
293+
$this->assertEquals('tags: [php, java]', $text);
287294
}
288295
}

0 commit comments

Comments
(0)

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