Skip to content

Commit 60908df

Browse files
committed
add new string util func and fix some error
1 parent 8909167 commit 60908df

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/Str/StringHelper.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use function preg_split;
3636
use function random_bytes;
3737
use function random_int;
38+
use function sprintf;
3839
use function str_pad;
3940
use function str_repeat;
4041
use function str_replace;
@@ -275,14 +276,33 @@ public static function genNOV2($prefix = '', array $randomRange = []): string
275276
}
276277

277278
/**
278-
* @param string $template
279+
* @param string $tplCode
279280
* @param array $vars
280281
*
281282
* @return string
282283
*/
283-
public static function replaces(string $template, array $vars): string
284+
public static function replaces(string $tplCode, array $vars): string
284285
{
285-
return strtr($template, $vars);
286+
return strtr($tplCode, $vars);
287+
}
288+
289+
/**
290+
* @param string $tplCode
291+
* @param array $vars
292+
* @param string $format
293+
*
294+
* @return string
295+
*/
296+
public static function renderTemplate(string $tplCode, array $vars, string $format = '{{%s}}'): string
297+
{
298+
$fmtVars = [];
299+
foreach ($vars as $name => $var) {
300+
$name = sprintf($format, (string)$name);
301+
// add
302+
$fmtVars[$name] = $var;
303+
}
304+
305+
return strtr($tplCode, $fmtVars);
286306
}
287307

288308
////////////////////////////////////////////////////////////
@@ -335,6 +355,30 @@ public static function removeQuotes(string $str): string
335355
return $str;
336356
}
337357

358+
/**
359+
* @param string $str
360+
* @param bool $quoteAll
361+
*
362+
* @return string
363+
*/
364+
public static function paramQuotes(string $str, bool $quoteAll = false): string
365+
{
366+
if ($str === '') {
367+
return "''";
368+
}
369+
370+
if (
371+
!$quoteAll &&
372+
(preg_match("/^\".*\"$/", $str) || preg_match("/^'.*'$/", $str))
373+
) {
374+
return $str;
375+
}
376+
377+
$quote = str_contains($str, "'") ? '"' : "'";
378+
379+
return $quote . $str . $quote;
380+
}
381+
338382
/**
339383
* @param array $list
340384
* @param string $wrapChar
@@ -377,7 +421,7 @@ public static function shellQuote(string $arg): string
377421
$quote = '';
378422
if (str_contains($arg, '"')) {
379423
$quote = "'";
380-
} elseif ($arg === '' || strpos($arg, "'") !== false || strpos($arg, ' ') !== false) {
424+
} elseif ($arg === '' || str_contains($arg, "'") || str_contains($arg, ' ')) {
381425
$quote = '"';
382426
}
383427

src/Str/Traits/StringCaseHelperTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public static function toCamelCase(string $str, bool $upperFirst = false): strin
227227
$str = self::ucfirst($str);
228228
}
229229

230-
return preg_replace_callback('/_+([a-z])/', static function ($c) {
230+
return preg_replace_callback('/[_-]+([a-z])/', static function ($c) {
231231
return strtoupper($c[1]);
232232
}, $str);
233233
}

test/Str/StringHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public function testToTyped(): void
5151
$this->assertEquals('true', Str::toTyped('true'));
5252
}
5353

54+
public function testParamQuotes(): void
55+
{
56+
$tests = [
57+
['', "''"],
58+
['abc', "'abc'"],
59+
["'abc'", "'abc'"],
60+
['ab" c', "'ab\" c'"],
61+
["ab' c", '"ab\' c"'],
62+
];
63+
foreach ($tests as [$given, $want]) {
64+
self::assertSame($want, Str::paramQuotes($given));
65+
}
66+
}
67+
5468
public function testShellQuote(): void
5569
{
5670
$tests = [

0 commit comments

Comments
 (0)