Skip to content

Commit f599bb1

Browse files
committed
add Helpers: array & string
1 parent fa4b4bf commit f599bb1

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/Exceptions/MissingConfigException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class MissingConfigException extends ConfigException
1111
{
12-
/** @var string */
12+
/** @var string|null */
1313
protected $needKey;
1414

1515
/**

src/Helpers/Arr.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Php\Support\Helpers;
4+
5+
/**
6+
* Class Arr
7+
*
8+
* @package Php\Support\Helpers
9+
*/
10+
class Arr
11+
{
12+
/**
13+
* Replace templates into array
14+
* Key = search value
15+
* Value = replace value
16+
*
17+
* @param array $array
18+
* @param array $replace
19+
*/
20+
public static function arrayReplaceByTemplate(array &$array, array $replace)
21+
{
22+
foreach ($array as &$item) {
23+
if (is_array($item)) {
24+
self::arrayReplaceByTemplate($item, $replace);
25+
} else if (is_string($item)) {
26+
$item = Str::stringReplaceByTemplate($item, $replace);
27+
}
28+
}
29+
}
30+
}

src/Helpers/Str.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Php\Support\Helpers;
4+
5+
/**
6+
* Class Str
7+
*
8+
* @package Php\Support\Helpers
9+
*/
10+
class Str
11+
{
12+
/**
13+
* Replace templates into string
14+
* Key = search value
15+
* Value = replace value
16+
*
17+
* @param string $str
18+
* @param array $replace
19+
*
20+
* @return mixed
21+
*/
22+
public static function stringReplaceByTemplate(string $str, array $replace)
23+
{
24+
return str_replace(array_keys($replace), array_values($replace), $str);
25+
}
26+
}

0 commit comments

Comments
 (0)