-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
StringUtils.php
126 lines (114 loc) · 4.1 KB
/
StringUtils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Drush\Utils;
class StringUtils
{
/**
* Convert a csv string, or an array of items which
* may contain csv strings, into an array of items.
*
* @param $args
* A simple csv string; e.g. 'a,b,c'
* or a simple list of items; e.g. array('a','b','c')
* or some combination; e.g. array('a,b','c') or array('a,','b,','c,').
*/
public static function csvToArray($args): array
{
//
// Step 1: implode(',',$args) converts from, say, array('a,','b,','c,') to 'a,,b,,c,'
// Step 2: explode(',', ...) converts to array('a','','b','','c','')
// Step 3: array_filter(...) removes the empty items
// Step 4: array_map(...) trims extra whitespace from each item
// (handles csv strings with extra whitespace, e.g. 'a, b, c')
//
$args = is_array($args) ? implode(',', array_map('strval', $args)) : (string) $args;
return array_map('trim', array_filter(explode(',', $args)));
}
/**
* Replace placeholders in a string.
*
* Examples:
* interpolate('Hello, {var}', ['var' => 'world']) ==> 'Hello, world'
* interpolate('Do !what', ['!what' => 'work']) ==> 'Do work'
*
* @param string $message
* The string with placeholders to be interpolated.
* @param array $context
* An associative array of values to be inserted into the message.
* The resulting string with all placeholders filled in.
*/
public static function interpolate(string $message, array $context = []): string
{
// Take no action if there is no context
if (empty($context)) {
return $message;
}
// build a replacement array with braces around the context keys
$replace = [];
foreach ($context as $key => $val) {
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace[static::interpolationKey($key)] = $val;
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
/**
* Wrap simple strings (with no special characters) in {}s
*
* @param string $key
* A key from an interpolation context.
* The key prepared for interpolation.
*/
private static function interpolationKey(string $key): string
{
if (ctype_alpha($key)) {
return sprintf('{%s}', $key);
}
return $key;
}
/**
* Replace tilde in a path with the HOME directory.
*
* @param $path
* A path that may contain a ~ at front.
*
* @param $home
* The effective home dir for this request.
*
* @return string The path with tilde replaced, if applicable.
* The path with tilde replaced, if applicable.
*/
public static function replaceTilde($path, $home)
{
$replacement = $home . '/';
$match = '#^~/#';
if (preg_match($match, $path)) {
return preg_replace($match, $replacement, $path);
}
return $path;
}
/**
* Generate a random alphanumeric password. Copied from user.module.
*
*
*/
public static function generatePassword(int $length = 10): string
{
// This variable contains the list of allowable characters for the
// password. Note that the number 0 and the letter 'O' have been
// removed to avoid confusion between the two. The same is true
// of 'I', 1, and 'l'.
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
// Zero-based count of characters in the allowable list:
$len = strlen($allowable_characters) - 1;
// Declare the password as a blank string.
$pass = '';
// Loop the number of times specified by $length.
for ($i = 0; $i < $length; $i++) {
// Each iteration, pick a random character from the
// allowable string and append it to the password:
$pass .= $allowable_characters[mt_rand(0, $len)];
}
return $pass;
}
}