| 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 |
|
| 141 |
if ( strlen( $text ) > $maxLength && ! empty( $maxLength ) && ! empty( $text ) ) { |
| 142 |
$words = preg_split('/\s/', $text ); |
| 143 |
$output = ''; |
| 144 |
$i = 0; |
| 145 |
while (1) { |
| 146 |
$length = strlen( $output ) + strlen( $words[$i] ); |
| 147 |
if ( $length > $maxLength ) { |
| 148 |
break; |
| 149 |
} |
| 150 |
else { |
| 151 |
$output .= " " . $words[$i]; |
| 152 |
++$i; |
| 153 |
} |
| 154 |
} |
| 155 |
$output .= $more; |
| 156 |
} |
| 157 |
else { |
| 158 |
$output = $text; |
| 159 |
} |
| 160 |
return $output; |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|