| 1 |
<?php |
| 2 |
namespace Averta\Core\Utility; |
| 3 |
|
| 4 |
|
| 5 |
class Str |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Convert string to camelCase |
| 9 |
* |
| 10 |
* @param string $input The string to convert |
| 11 |
* @param string $separator The character that should be removed |
| 12 |
* @param bool $capitalizeFirstChar Whether to capitalize the first character or not |
| 13 |
* |
| 14 |
* @return string |
| 15 |
*/ |
| 16 |
public static function camelize( $input, $separator = "_", $capitalizeFirstChar = false ) { |
| 17 |
|
| 18 |
$string = str_replace( $separator, '', ucwords( $input, $separator ) ); |
| 19 |
|
| 20 |
if ( ! $capitalizeFirstChar ) { |
| 21 |
$string = lcfirst( $string ); |
| 22 |
} |
| 23 |
|
| 24 |
return $string; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Generates and trims a persistent simple hash |
| 29 |
* |
| 30 |
* @param string $data The input string. |
| 31 |
* @param int $start If offset is negative, the returned string will start at the offset'th character from the end of string. |
| 32 |
* @param int $length If length is given and is positive, the string returned will contain at most length characters beginning from start. |
| 33 |
* |
| 34 |
* @return false|string |
| 35 |
*/ |
| 36 |
public static function simpleHash( $data, $start = 0, $length = 10 ){ |
| 37 |
return self::hash( $algorithm = 'md5' , $data, $start, $length ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generates and trims a hash value |
| 42 |
* |
| 43 |
* @param string $algorithm Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..) |
| 44 |
* @param string $data The input string. |
| 45 |
* @param int $start If offset is negative, the returned string will start at the offset'th character from the end of string. |
| 46 |
* @param int $length If length is given and is positive, the string returned will contain at most length characters beginning from start. |
| 47 |
* |
| 48 |
* @return false|string |
| 49 |
*/ |
| 50 |
public static function hash( $algorithm , $data, $start = 0, $length = 100 ){ |
| 51 |
return substr( hash( $algorithm, $data, false ), $start, $length ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Generates a persistent simple short hash |
| 56 |
* |
| 57 |
* @param string $data The input string. |
| 58 |
* @param bool $binary |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public static function shortHash( $data, $binary = false ){ |
| 63 |
return hash( 'adler32', $data, $binary ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Extracts and returns a part of string based on provided regex. |
| 68 |
* |
| 69 |
* @param string $string String to search in by regex |
| 70 |
* @param string $regex Regular expression for extracting a part of URL |
| 71 |
* @param int $matchIndex Which part of matched part we are looking for. Set -1 to get all matches. |
| 72 |
* |
| 73 |
* @return bool|mixed Returns the matched part on success, and false on failure. |
| 74 |
*/ |
| 75 |
public static function extractByRegex( $string, $regex, $matchIndex = 1 ){ |
| 76 |
|
| 77 |
$matches = []; |
| 78 |
|
| 79 |
preg_match( |
| 80 |
$regex, |
| 81 |
$string, |
| 82 |
$matches |
| 83 |
); |
| 84 |
|
| 85 |
if( $matchIndex === -1 ){ |
| 86 |
return $matches; |
| 87 |
} |
| 88 |
|
| 89 |
if( isset( $matches[ $matchIndex ] ) ){ |
| 90 |
return $matches[ $matchIndex ]; |
| 91 |
} |
| 92 |
|
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Converts semantic version to decimal |
| 98 |
* |
| 99 |
* @param string $version |
| 100 |
* |
| 101 |
* @return int |
| 102 |
*/ |
| 103 |
public static function versionToDecimal( $version = '0.0.0' ) { |
| 104 |
$result = ''; |
| 105 |
$versionParts = explode( '.', $version ); |
| 106 |
foreach( $versionParts as $value ) { |
| 107 |
$result .= substr( "00" . $value, -3 ); |
| 108 |
} |
| 109 |
return (int) $result; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Trims a text by character length |
| 114 |
* |
| 115 |
* @param string $text The text to be trimmed |
| 116 |
* @param int $maxLength Number of characters in text. |
| 117 |
* @param string $more What to append if $text needs to be trimmed. Default ' …' |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
public static function trimByChars( $text, $maxLength = 1000, $more = " ..." ){ |
| 122 |
$textLength = function_exists('mb_strwidth') ? mb_strwidth( $text ) : strlen( $text ); |
| 123 |
|
| 124 |
if( $textLength > $maxLength && ! empty( $maxLength ) && ! empty( $text ) ){ |
| 125 |
return function_exists( 'mb_strimwidth' ) ? mb_strimwidth( $text, 0, $maxLength, '' ) . $more : substr( $text, 0, $maxLength ) . $more; |
| 126 |
} |
| 127 |
return $text; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Trims text to a certain number of words. |
| 132 |
* |
| 133 |
* @param string $text The text to be trimmed |
| 134 |
* @param int $maxLength Number of words. |
| 135 |
* @param string $more What to append if $text needs to be trimmed. Default ' …'. |
| 136 |
* |
| 137 |
* @return mixed|string |
| 138 |
*/ |
| 139 |
public static function trimByWords( $text, $maxLength, $more = " ..." ) { |
| 140 |
$words = preg_split('/\s+/u', $text, $maxLength + 1); |
| 141 |
if( count( $words ) > $maxLength ) { |
| 142 |
array_pop($words); |
| 143 |
$text = implode(' ', $words); |
| 144 |
$text .= $more; |
| 145 |
} |
| 146 |
return $text; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Removes a string from the end of a text |
| 151 |
* |
| 152 |
* @param string $text the original text |
| 153 |
* @param string $stringToTrim the string that should be removed from end of original text |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public static function rightTrim( $text, $stringToTrim ) { |
| 158 |
$lengthToTrim = strlen( $stringToTrim ); |
| 159 |
$originalText = $text; |
| 160 |
|
| 161 |
$text = trim( $text ); |
| 162 |
|
| 163 |
if( substr( $text, -$lengthToTrim ) == $stringToTrim ) { |
| 164 |
return substr( $text, 0, -$lengthToTrim ); |
| 165 |
} |
| 166 |
|
| 167 |
return $originalText; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Removes a string from the start of a text |
| 172 |
* |
| 173 |
* @param string $text the original text |
| 174 |
* @param string $stringToTrim the string that should be removed from start of original text |
| 175 |
* |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public static function leftTrim( $text, $stringToTrim ) { |
| 179 |
if ( strpos( $text, $stringToTrim ) === 0) { |
| 180 |
return substr( $text, strlen( $stringToTrim ) ); |
| 181 |
} |
| 182 |
|
| 183 |
return $text; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Check if string ends with another string |
| 188 |
* |
| 189 |
* @param $haystack |
| 190 |
* @param $needle |
| 191 |
* |
| 192 |
* @return bool |
| 193 |
*/ |
| 194 |
public static function ends( $haystack, $needle ){ |
| 195 |
$len = strlen( $needle ); |
| 196 |
if ( $len == 0 ) { |
| 197 |
return true; |
| 198 |
} |
| 199 |
return substr( $haystack, -$len ) === $needle; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Check if string starts with another string |
| 204 |
* |
| 205 |
* @param $haystack |
| 206 |
* @param $needle |
| 207 |
* |
| 208 |
* @return bool |
| 209 |
*/ |
| 210 |
public static function starts( $haystack, $needle ){ |
| 211 |
return 0 === strpos( $haystack, $needle ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Converts comma-separated string, empty, or null values to array |
| 216 |
* |
| 217 |
* @param mixed $value Variable to be converted |
| 218 |
* @param string $separator Separator in text-separated string |
| 219 |
* @return array |
| 220 |
*/ |
| 221 |
public static function toArray( $value, $separator = ',' ){ |
| 222 |
if ( is_null( $value ) || $value === '') { |
| 223 |
return []; |
| 224 |
} elseif ( is_array( $value ) ) { |
| 225 |
return $value; |
| 226 |
} elseif ( is_string( $value ) ) { |
| 227 |
$value = array_map('trim', explode( $separator, $value ) ); |
| 228 |
// remove empty items as well |
| 229 |
return array_filter( $value, function( $item ) { |
| 230 |
return !empty( $item ); |
| 231 |
}); |
| 232 |
} else { |
| 233 |
return []; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|