| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ronanchilvers\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Useful string manipulation methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * NB: This class only handles english strings - it is not aware of other languages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     static protected $irregulars = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'child'  => 'children', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'foot'   => 'feet', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'goose'  => 'geese', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'man'    => 'men', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'mouse'  => 'mice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'person' => 'people', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'sheep'  => 'sheep', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'tooth'  => 'teeth', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     static protected $uncountable = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'sheep', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'fish', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'deer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'series', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'species', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'money', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'rice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'information', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'equipment' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     static protected $vowels = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'a', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         'e', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         'i', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         'o', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         'u' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Pluralise a string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * [1.0] 'ies' rule    (ends in a consonant + y : baby/lady) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * [2.0] 'ves' rule    (ends in f or fe : leaf/knife) --- roof : rooves (correct but old english, roofs is ok). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * [3.1] 'es' rule 1   (ends in a consonant + o : volcano/mango) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * [3.2] 'es' rule 2   (ends in ch, sh, s, ss, x, z : match/dish/bus/glass/fox/buzz) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * [4.1] 's' rule 1    (ends in a vowel + y or o : boy/radio) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * [4.2] 's' rule 2    (ends in other than above : cat/ball) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $string The singular noun to pluralise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param int $count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $plural | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     static public function plural($string, $count = 2, $plural = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $string = trim($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (in_array($string, static::$uncountable) && false === $plural) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (empty($string) || $count == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (false !== $plural) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return $plural; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $string = mb_strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (isset(static::$irregulars[$string])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return static::$irregulars[$string]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // [1.0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (!in_array(mb_substr($string, -2, 1), static::$vowels) && 'y' == mb_substr($string, -1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return mb_substr($string, 0, -1) . 'ies'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // [2.0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if ('f' == mb_substr($string, -1) || 'fe' == mb_substr($string, -2)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $length = ('e' == mb_substr($string, -1)) ? -2 : -1 ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return mb_substr($string, 0, $length) . 'ves'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         // [3.1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (!in_array(mb_substr($string, -2, 1), static::$vowels) && 'o' == mb_substr($string, -1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return $string . 'es'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // [3.2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (in_array(mb_substr($string, -2), ['ch', 'sh', 'ss']) || in_array(mb_substr($string, -1), ['s', 'x', 'z'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return $string . 'es'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $string . 's'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Singularise a string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * [1.0] 'ies' rule    (ends in a consonant + y : baby/lady) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * [2.0] 'ves' rule    (ends in f or fe : leaf/knife) --- roof : rooves (correct but old english, roofs is ok). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * [3.1] 'es' rule 1   (ends in a consonant + o : volcano/mango) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * [3.2] 'es' rule 2   (ends in ch, sh, s, ss, x, z : match/dish/bus/glass/fox/buzz) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * [4.1] 's' rule 1    (ends in a vowel + y or o : boy/radio) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * [4.2] 's' rule 2    (ends in other than above : cat/ball) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $string The singular noun to singularise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param int $count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param string $singular | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     static public function singular($string, $count = 1, $singular = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $string = trim($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (in_array($string, static::$uncountable) && false === $singular) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if (empty($string) || $count !== 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (false !== $singular) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             return $singular; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $singulars = array_flip(static::$irregulars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $string = mb_strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if (isset($singulars[$string])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return $singulars[$string]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         // [1.0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         if ('ies' == mb_substr($string, -3)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             return mb_substr($string, 0, -3) . 'y'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // [2.0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         if ('ves' == mb_substr($string, -3)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             if (in_array(mb_substr($string, -4, 1), static::$vowels)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 return mb_substr($string, 0, -3) . 'f'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         // [3.1, 3.2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         if ('ves' != mb_substr($string, -3) && 'es' == mb_substr($string, -2)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             return mb_substr($string, 0, -2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return rtrim($string, 's'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * Convert a string to PascalCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * Pascal case is upper camel case, or camel case with the first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * letter uppercased. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param array $allowed Allowed characters that won't be compressed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     static public function pascal($string, $allowed = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $string = mb_strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $string = preg_replace('#[^a-z0-9' . implode('', $allowed) . ']+#', ' ', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $string = str_replace(' ', '', ucwords($string)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * Convert a string to camelCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * We're taking camel case here to mean lower camel case, ie: the first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * letter is lower case. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param array $allowed Allowed characters that won't be compressed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     static public function camel($string, $allowed = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         return lcfirst( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             static::pascal($string, $allowed) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * Snake case a string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * This method snake cases phrases and can also convert camelCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * and PascalCase strings to snake case. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      * @param array $allowed Allowed characters that won't be compressed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     static public function snake($string, $allowed = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         // $string = mb_strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         $string = preg_replace('#([A-Z]{1})#', ' $1', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         $string = mb_strtolower(trim($string)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         $string = preg_replace('#[^a-z0-9' . implode('', $allowed) . ']{1,}#', ' ', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         $string = str_replace(' ', '_', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      * Truncate a string to a given length, optionally respecting word | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * boundaries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * @param string $string The string to truncate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * @param int $length The length to truncate to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @param string $suffix Suffix to tag on the end of the string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @param boolean $words Respect word boundaries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      * @param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |     static public function truncate($string, $length, $suffix = '...', $words = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         if ($length > mb_strlen($string)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         if (false === $words) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             return mb_substr($string, 0, $length - mb_strlen($suffix)) . $suffix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         $matches = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         preg_match( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |             "/(^.{1," . ($length - mb_strlen($suffix)) . "})(?=\s|$).*/u", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |             $string, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             $matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         if (!isset($matches[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             return mb_substr($string, 0, $length - mb_strlen($suffix)) . $suffix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         return $matches[1] . $suffix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      * Generate a long random string suitable for use as a token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |      * @param int $length | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 269 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |     static public function token($length = 64) | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |         return substr(bin2hex( | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |             random_bytes($length) | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |         ), 0, $length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |      * Create a string by joining an arbitrary number of other strings with a separator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |      * @param string $seperator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |      * @param string $piece... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |     static public function join($seperator, ...$pieces) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         foreach ($pieces as &$piece) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             $piece = trim($piece, $seperator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         return implode($seperator, $pieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |      * Convert a string to a boolean value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * @param string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     static public function bool($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         if (true === $string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |         switch ($string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             case '1': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |             case 'yes': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |             case 'true': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |      * Simple moustaches templating | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |      * @param string $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |     static public function moustaches(string $template, array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         $keys = array_map(function ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |             return '{'.$value.'}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         }, array_keys($params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |         $values = array_values($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         return str_replace($keys, $values, $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |      * Normalise a string into a key with only hyphens and lowercase alphanumeric characters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |      * @author Ronan Chilvers <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |     static public function normalise($string): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |         $string = strtolower($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |         $string = preg_replace('/[^A-z0-9\s-]+/', '', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |         $string = preg_replace('/[\s-]{1,}/', '-', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 351 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 352 |  |  |  |