Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Utils / StringUtils.php
1 <?php
2
3 namespace Drush\Utils;
4
5 use Drush\Drush;
6
7 class StringUtils
8 {
9
10     /**
11      * Convert a csv string, or an array of items which
12      * may contain csv strings, into an array of items.
13      *
14      * @param $args
15      *   A simple csv string; e.g. 'a,b,c'
16      *   or a simple list of items; e.g. array('a','b','c')
17      *   or some combination; e.g. array('a,b','c') or array('a,','b,','c,')
18      *
19      * @return array
20      *   A simple list of items (e.g. array('a','b','c')
21      */
22     public static function csvToArray($args)
23     {
24         //
25         // Step 1: implode(',',$args) converts from, say, array('a,','b,','c,') to 'a,,b,,c,'
26         // Step 2: explode(',', ...) converts to array('a','','b','','c','')
27         // Step 3: array_filter(...) removes the empty items
28         // Step 4: array_map(...) trims extra whitespace from each item
29         // (handles csv strings with extra whitespace, e.g. 'a, b, c')
30         //
31         return array_map('trim', array_filter(explode(',', is_array($args) ? implode(',', $args) : $args)));
32     }
33
34     /**
35      * Replace placeholders in a string.
36      *
37      * Examples:
38      *   interpolate('Hello, {var}', ['var' => 'world']) ==> 'Hello, world'
39      *   interpolate('Do !what', ['!what' => 'work'])    ==> 'Do work'
40      *
41      * @param string $message
42      *   The string with placeholders to be interpolated.
43      * @param array $context
44      *   An associative array of values to be inserted into the message.
45      * @return string
46      *   The resulting string with all placeholders filled in.
47      */
48     public static function interpolate($message, array $context = [])
49     {
50         // Take no action if there is no context
51         if (empty($context)) {
52             return $message;
53         }
54
55         // build a replacement array with braces around the context keys
56         $replace = [];
57         foreach ($context as $key => $val) {
58             if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
59                 $replace[static::interpolationKey($key)] = $val;
60             }
61         }
62
63         // interpolate replacement values into the message and return
64         return strtr($message, $replace);
65     }
66
67     /**
68      * Wrap simple strings (with no special characters) in {}s
69      *
70      * @param string $key
71      *   A key from an interpolation context.
72      * @return string
73      *   The key prepared for interpolation.
74      */
75     private static function interpolationKey($key)
76     {
77         if (ctype_alpha($key)) {
78             return sprintf('{%s}', $key);
79         }
80         return $key;
81     }
82
83     /**
84      * Replace tilde in a path with the HOME directory.
85      *
86      * @param $path
87      *   A path that may contain a ~ at front.
88      *
89      * @param $home
90      *   The effective home dir for this request.
91      *
92      * @return string The path with tilde replaced, if applicable.
93      * The path with tilde replaced, if applicable.
94      */
95     public static function replaceTilde($path, $home)
96     {
97         $replacement = $home . '/';
98         $match = '#^~/#';
99         if (preg_match($match, $path)) {
100             return preg_replace($match, $replacement, $path);
101         }
102         return $path;
103     }
104
105   /**
106    * Generate a random alphanumeric password.  Copied from user.module.
107    *
108    * @param int $length
109    *
110    * @return string
111    */
112     public static function generatePassword($length = 10)
113     {
114         // This variable contains the list of allowable characters for the
115         // password. Note that the number 0 and the letter 'O' have been
116         // removed to avoid confusion between the two. The same is true
117         // of 'I', 1, and 'l'.
118         $allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
119
120         // Zero-based count of characters in the allowable list:
121         $len = strlen($allowable_characters) - 1;
122
123         // Declare the password as a blank string.
124         $pass = '';
125
126         // Loop the number of times specified by $length.
127         for ($i = 0; $i < $length; $i++) {
128             // Each iteration, pick a random character from the
129             // allowable string and append it to the password:
130             $pass .= $allowable_characters[mt_rand(0, $len)];
131         }
132
133         return $pass;
134     }
135 }