This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
60 lines (56 loc) · 1.74 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
if (!function_exists('e')) {
/**
* Short alias for htmlentities(). This function is identical to htmlspecialchars() in all ways,
* except with htmlentities(), all characters which have HTML character entity equivalents are
* translated into these entities.
*
* @param mixed $string
* @param bool $stripTags
* @return string
*/
function e($string = null, bool $stripTags = false): string
{
return \Spiral\Helpers\Strings::escape($string, $stripTags);
}
}
if (!function_exists('interpolate')) {
/**
* Interpolate string with given parameters, used by many spiral components.
*
* Input: Hello {name}! Good {time}! + ['name' => 'Member', 'time' => 'day']
* Output: Hello Member! Good Day!
*
* @param string $string
* @param array $values Arguments (key => value). Will skip unknown names.
* @param string $prefix Placeholder prefix, "{" by default.
* @param string $postfix Placeholder postfix, "}" by default.
*
* @return mixed
*/
function interpolate(
string $string,
array $values,
string $prefix = '{',
string $postfix = '}'
): string {
$replaces = [];
foreach ($values as $key => $value) {
$value = (is_array($value) || $value instanceof \Closure) ? '' : $value;
try {
//Object as string
$value = is_object($value) ? (string)$value : $value;
} catch (\Exception $e) {
$value = '';
}
$replaces[$prefix . $key . $postfix] = $value;
}
return strtr($string, $replaces);
}
}