Facades
3 weeks ago
Traits
3 weeks ago
Arr.php
3 weeks ago
DataCaster.php
3 weeks ago
Flex.php
3 weeks ago
HigherOrderTapProxy.php
3 weeks ago
MediaAttachment.php
3 weeks ago
MessagesBag.php
3 weeks ago
Str.php
3 weeks ago
Url.php
3 weeks ago
Utils.php
3 weeks ago
Str.php
488 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * A utility class for string manipulation and formatting. |
| 5 | * Provides various static methods to handle common string operations such as type conversion, |
| 6 | * slug generation, and string incrementation. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Supports |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Supports; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Sanitizer; |
| 16 | use Kirki\Framework\Supports\Traits\Macroable; |
| 17 | use Traversable; |
| 18 | use function Kirki\Framework\Polyfill\array_first; |
| 19 | use function Kirki\Framework\Polyfill\array_last; |
| 20 | use function Kirki\Framework\Polyfill\str_starts_with; |
| 21 | class Str |
| 22 | { |
| 23 | use Macroable; |
| 24 | /** |
| 25 | * The increment styles. |
| 26 | * |
| 27 | * @var array |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | protected static $increment_styles = ['dash' => ['/-(\\d+)$/', '-%d'], 'regular' => [['/\\((\\d+)\\)$/', '/\\(\\d+\\)$/'], [' (%d)', '(%d)']]]; |
| 32 | /** |
| 33 | * Convert a numeric string to a corresponding number. |
| 34 | * |
| 35 | * @param mixed $value The value. |
| 36 | * |
| 37 | * @return string|int|float |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function to_number($value) |
| 42 | { |
| 43 | if (empty($value) || !\is_numeric($value)) { |
| 44 | return $value; |
| 45 | } |
| 46 | return \strpos($value, '.') !== \false || \stripos($value, 'e') !== \false ? (float) $value : (int) $value; |
| 47 | } |
| 48 | /** |
| 49 | * Sanitize a string to use as a slug. |
| 50 | * |
| 51 | * This function sanitizes the input string according to the Sanitizer::TITLE rule, |
| 52 | * which is used to sanitize title strings. |
| 53 | * |
| 54 | * @param string $value The string to sanitize. |
| 55 | * |
| 56 | * @return string The sanitized string. |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public static function slug($value) |
| 61 | { |
| 62 | return Sanitizer::apply_rule($value, Sanitizer::TITLE); |
| 63 | } |
| 64 | /** |
| 65 | * Increment the string. |
| 66 | * |
| 67 | * @param string $value The string to increment. |
| 68 | * @param string $style The style of the increment. Available styles are 'dash' and 'regular'. |
| 69 | * |
| 70 | * @return string The incremented string. |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | */ |
| 74 | public static function increment($value, $style = 'dash') |
| 75 | { |
| 76 | $style = static::$increment_styles[$style] ?? static::$increment_styles['regular']; |
| 77 | if (\is_array($style[0])) { |
| 78 | $search_regex = array_first($style[0]); |
| 79 | $replace_regex = array_last($style[0]); |
| 80 | } else { |
| 81 | $search_regex = $replace_regex = $style[0]; |
| 82 | } |
| 83 | if (\is_array($style[1])) { |
| 84 | $old_format = array_first($style[1]); |
| 85 | $new_format = array_last($style[1]); |
| 86 | } else { |
| 87 | $old_format = $new_format = $style[1]; |
| 88 | } |
| 89 | if (\preg_match($search_regex, $value, $matches)) { |
| 90 | $count = \intval($matches[1]) + 1; |
| 91 | return \preg_replace($replace_regex, \sprintf($new_format, $count), $value, 1); |
| 92 | } else { |
| 93 | $count = 2; |
| 94 | return $value . \sprintf($old_format, $count); |
| 95 | } |
| 96 | } |
| 97 | /** |
| 98 | * Trim the string. |
| 99 | * |
| 100 | * @param string $value The string to trim. |
| 101 | * @param string $charlist The characters to trim. |
| 102 | * |
| 103 | * @return string The trimmed string. |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | public static function trim($value, $charlist = null) |
| 108 | { |
| 109 | if (\is_null($charlist)) { |
| 110 | $trim_default_chars = " \n\r\t\v\x00"; |
| 111 | $quoted_chars = \preg_quote($trim_default_chars, '~'); |
| 112 | $regex = '~^[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+|[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+$~u'; |
| 113 | return \preg_replace($regex, '', $value) ?? \trim($value); |
| 114 | } |
| 115 | return \trim($value, $charlist); |
| 116 | } |
| 117 | /** |
| 118 | * Left trim the string. |
| 119 | * |
| 120 | * @param string $value The string to left trim. |
| 121 | * @param string $charlist The characters to left trim. |
| 122 | * |
| 123 | * @return string The left trimmed string. |
| 124 | * |
| 125 | * @since 1.0.0 |
| 126 | */ |
| 127 | public static function ltrim($value, $charlist = null) |
| 128 | { |
| 129 | if (\is_null($charlist)) { |
| 130 | $ltrim_default_chars = " \n\r\t\v\x00"; |
| 131 | $quoted_chars = \preg_quote($ltrim_default_chars, '~'); |
| 132 | $regex = '~^[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+~u'; |
| 133 | return \preg_replace($regex, '', $value) ?? \ltrim($value); |
| 134 | } |
| 135 | return \ltrim($value, $charlist); |
| 136 | } |
| 137 | /** |
| 138 | * Right trim the string. |
| 139 | * |
| 140 | * @param string $value The string to right trim. |
| 141 | * @param string $charlist The characters to right trim. |
| 142 | * |
| 143 | * @return string The right trimmed string. |
| 144 | * |
| 145 | * @since 1.0.0 |
| 146 | */ |
| 147 | public static function rtrim($value, $charlist = null) |
| 148 | { |
| 149 | if (\is_null($charlist)) { |
| 150 | $rtrim_default_chars = " \n\r\t\v\x00"; |
| 151 | $quoted_chars = \preg_quote($rtrim_default_chars, '~'); |
| 152 | $regex = '~[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+$~u'; |
| 153 | return \preg_replace($regex, '', $value) ?? \rtrim($value); |
| 154 | } |
| 155 | return \rtrim($value, $charlist); |
| 156 | } |
| 157 | /** |
| 158 | * Squish the string. |
| 159 | * |
| 160 | * @param string $value The string to squish. |
| 161 | * |
| 162 | * @return string The squished string. |
| 163 | * |
| 164 | * @since 1.0.0 |
| 165 | */ |
| 166 | public static function squish($value) |
| 167 | { |
| 168 | return \preg_replace('~(\\s|\\x{3164}|\\x{1160})+~u', ' ', static::trim($value)); |
| 169 | } |
| 170 | /** |
| 171 | * Get a substring of the string. |
| 172 | * |
| 173 | * @param string $value The string to get a substring from. |
| 174 | * @param int $start The starting position of the substring. |
| 175 | * @param int $length The length of the substring. |
| 176 | * @param string $encoding The encoding of the string. |
| 177 | * |
| 178 | * @return string The substring. |
| 179 | * |
| 180 | * @since 1.0.0 |
| 181 | */ |
| 182 | public static function substr($value, $start, $length = null, $encoding = 'UTF-8') |
| 183 | { |
| 184 | return \mb_substr($value, $start, $length, $encoding); |
| 185 | } |
| 186 | /** |
| 187 | * Get a substring of the string. |
| 188 | * |
| 189 | * @param string $value The string to get a substring from. |
| 190 | * @param int $limit The limit of the substring. |
| 191 | * |
| 192 | * @return string The substring. |
| 193 | * |
| 194 | * @since 1.0.0 |
| 195 | */ |
| 196 | public static function take($value, int $limit) |
| 197 | { |
| 198 | if ($limit < 0) { |
| 199 | return static::substr($value, $limit); |
| 200 | } |
| 201 | return static::substr($value, 0, $limit); |
| 202 | } |
| 203 | /** |
| 204 | * Convert the string to base64. |
| 205 | * |
| 206 | * @param string $value The string to convert to base64. |
| 207 | * |
| 208 | * @return string The base64 string. |
| 209 | * |
| 210 | * @since 1.0.0 |
| 211 | */ |
| 212 | public static function to_base64($value) |
| 213 | { |
| 214 | return \base64_encode($value); |
| 215 | } |
| 216 | /** |
| 217 | * Uuid. |
| 218 | * |
| 219 | * @return void |
| 220 | * |
| 221 | * @since 1.0.0 |
| 222 | */ |
| 223 | public static function uuid() |
| 224 | { |
| 225 | return wp_generate_uuid4(); |
| 226 | } |
| 227 | /** |
| 228 | * Replace the string. |
| 229 | * |
| 230 | * @param string|array $search The string to search for. |
| 231 | * @param string|array $replace The string to replace with. |
| 232 | * @param string|array $subject The string to replace in. |
| 233 | * @param bool $case_sensitive Whether to perform a case-sensitive search. |
| 234 | * |
| 235 | * @return string The replaced string. |
| 236 | * |
| 237 | * @since 1.0.0 |
| 238 | */ |
| 239 | public static function replace($search, $replace, $subject, $case_sensitive = \true) |
| 240 | { |
| 241 | if ($search instanceof Traversable) { |
| 242 | $search = Arr::from($search); |
| 243 | } |
| 244 | if ($replace instanceof Traversable) { |
| 245 | $replace = Arr::from($replace); |
| 246 | } |
| 247 | if ($subject instanceof Traversable) { |
| 248 | $subject = Arr::from($subject); |
| 249 | } |
| 250 | return $case_sensitive ? \str_replace($search, $replace, $subject) : \str_ireplace($search, $replace, $subject); |
| 251 | } |
| 252 | /** |
| 253 | * Convert the string to uppercase. |
| 254 | * |
| 255 | * @param string $value The string to convert to uppercase. |
| 256 | * |
| 257 | * @return string The uppercase string. |
| 258 | * |
| 259 | * @since 1.0.0 |
| 260 | */ |
| 261 | public static function upper($value) |
| 262 | { |
| 263 | return \mb_strtoupper($value, 'UTF-8'); |
| 264 | } |
| 265 | /** |
| 266 | * Convert the string to lowercase. |
| 267 | * |
| 268 | * @param string $value The string to convert to lowercase. |
| 269 | * |
| 270 | * @return string The lowercase string. |
| 271 | * |
| 272 | * @since 1.0.0 |
| 273 | */ |
| 274 | public static function lower($value) |
| 275 | { |
| 276 | return \mb_strtolower($value, 'UTF-8'); |
| 277 | } |
| 278 | /** |
| 279 | * Convert the first character of the string to uppercase. |
| 280 | * |
| 281 | * @param string $value The string to convert. |
| 282 | * |
| 283 | * @return string The string with the first character converted to uppercase. |
| 284 | * |
| 285 | * @since 1.0.0 |
| 286 | */ |
| 287 | public static function ucfirst($value) |
| 288 | { |
| 289 | return static::upper(static::substr($value, 0, 1)) . static::substr($value, 1); |
| 290 | } |
| 291 | /** |
| 292 | * Convert the first character of the string to lowercase. |
| 293 | * |
| 294 | * @param string $value The string to convert. |
| 295 | * |
| 296 | * @return string The string with the first character converted to lowercase. |
| 297 | * |
| 298 | * @since 1.0.0 |
| 299 | */ |
| 300 | public static function lcfirst($value) |
| 301 | { |
| 302 | return static::lower(static::substr($value, 0, 1)) . static::substr($value, 1); |
| 303 | } |
| 304 | /** |
| 305 | * Convert the first character of each word in the string to uppercase. |
| 306 | * |
| 307 | * @param string $value The string to convert. |
| 308 | * @param string $separators The characters to consider as word separators. |
| 309 | * |
| 310 | * @return string The string with the first character of each word converted to uppercase. |
| 311 | * |
| 312 | * @since 1.0.0 |
| 313 | */ |
| 314 | public static function ucwords($value, $separators = " \t\r\n\f\v") |
| 315 | { |
| 316 | $pattern = '/(^|[' . \preg_quote($separators, '/') . '])(\\p{Ll})/u'; |
| 317 | return \preg_replace_callback($pattern, function ($matches) { |
| 318 | return $matches[1] . \mb_strtoupper($matches[2]); |
| 319 | }, $value); |
| 320 | } |
| 321 | /** |
| 322 | * Convert the string to pascal case. |
| 323 | * |
| 324 | * @param string $value The string to convert. |
| 325 | * |
| 326 | * @return string The pascal case string. |
| 327 | * |
| 328 | * @since 1.0.0 |
| 329 | */ |
| 330 | public static function pascal($value) |
| 331 | { |
| 332 | $words = \mb_split('\\s+', static::replace(['-', '_'], ' ', $value)); |
| 333 | return \implode(\array_map(function ($word) { |
| 334 | return static::ucfirst($word); |
| 335 | }, $words)); |
| 336 | } |
| 337 | /** |
| 338 | * Convert the string to camel case. |
| 339 | * |
| 340 | * @param string $value The string to convert. |
| 341 | * |
| 342 | * @return string The camel case string. |
| 343 | * |
| 344 | * @since 1.0.0 |
| 345 | */ |
| 346 | public static function camel($value) |
| 347 | { |
| 348 | return static::lcfirst(static::pascal($value)); |
| 349 | } |
| 350 | /** |
| 351 | * Convert the string to snake case. |
| 352 | * |
| 353 | * @param string $value The string to convert. |
| 354 | * |
| 355 | * @return string The snake case string. |
| 356 | * |
| 357 | * @since 1.0.0 |
| 358 | */ |
| 359 | public static function snake($value) |
| 360 | { |
| 361 | return static::lower(static::replace([' ', '-'], '_', $value)); |
| 362 | } |
| 363 | /** |
| 364 | * Determine if a given string starts with a given substring. |
| 365 | * |
| 366 | * @param string $haystack The string to search in. |
| 367 | * @param string|array $needles The substring to search for. |
| 368 | * |
| 369 | * @return bool Whether the string starts with the substring. |
| 370 | * |
| 371 | * @since 1.0.0 |
| 372 | */ |
| 373 | public static function starts_with($haystack, $needles) |
| 374 | { |
| 375 | if (\is_null($haystack)) { |
| 376 | return \false; |
| 377 | } |
| 378 | $needles = Arr::wrap($needles); |
| 379 | foreach ($needles as $needle) { |
| 380 | if (!empty((string) $needle) && str_starts_with($haystack, $needle)) { |
| 381 | return \true; |
| 382 | } |
| 383 | } |
| 384 | return \false; |
| 385 | } |
| 386 | /** |
| 387 | * Split the string into an array. |
| 388 | * |
| 389 | * @param string $separator The separator to use. |
| 390 | * @param string $value The string to split. |
| 391 | * @param bool $case_sensitive Whether to perform a case-sensitive search. |
| 392 | * |
| 393 | * @return array The array of strings. |
| 394 | * |
| 395 | * @since 1.0.0 |
| 396 | */ |
| 397 | public static function split(string $separator, string $value, bool $case_sensitive = \false) |
| 398 | { |
| 399 | $pattern = "/" . \preg_quote($separator, '/'); |
| 400 | $pattern .= $case_sensitive ? '/' : '/i'; |
| 401 | return \preg_split($pattern, $value); |
| 402 | } |
| 403 | /** |
| 404 | * After. |
| 405 | * |
| 406 | * @param string $subject The subject. |
| 407 | * @param string $search The search. |
| 408 | * |
| 409 | * @return void |
| 410 | * |
| 411 | * @since 1.0.0 |
| 412 | */ |
| 413 | public static function after(string $subject, string $search) |
| 414 | { |
| 415 | if (empty($search)) { |
| 416 | return $subject; |
| 417 | } |
| 418 | $position = \strpos($subject, $search); |
| 419 | if ($position === \false) { |
| 420 | return $subject; |
| 421 | } |
| 422 | return \substr($subject, $position + \strlen($search)); |
| 423 | } |
| 424 | /** |
| 425 | * Before. |
| 426 | * |
| 427 | * @param string $subject The subject. |
| 428 | * @param string $search The search. |
| 429 | * |
| 430 | * @return void |
| 431 | * |
| 432 | * @since 1.0.0 |
| 433 | */ |
| 434 | public static function before(string $subject, string $search) |
| 435 | { |
| 436 | if (empty($search)) { |
| 437 | return $subject; |
| 438 | } |
| 439 | $position = \strpos($subject, $search); |
| 440 | if ($position === \false) { |
| 441 | return $subject; |
| 442 | } |
| 443 | return \substr($subject, 0, $position); |
| 444 | } |
| 445 | /** |
| 446 | * After last. |
| 447 | * |
| 448 | * @param string $subject The subject. |
| 449 | * @param string $search The search. |
| 450 | * |
| 451 | * @return string |
| 452 | * |
| 453 | * @since 1.0.0 |
| 454 | */ |
| 455 | public static function after_last(string $subject, string $search) |
| 456 | { |
| 457 | if (empty($search)) { |
| 458 | return $subject; |
| 459 | } |
| 460 | $position = \strrpos($subject, $search); |
| 461 | if ($position === \false) { |
| 462 | return $subject; |
| 463 | } |
| 464 | return \substr($subject, $position + \strlen($search)); |
| 465 | } |
| 466 | /** |
| 467 | * Before last. |
| 468 | * |
| 469 | * @param string $subject The subject. |
| 470 | * @param string $search The search. |
| 471 | * |
| 472 | * @return string |
| 473 | * |
| 474 | * @since 1.0.0 |
| 475 | */ |
| 476 | public static function before_last(string $subject, string $search) |
| 477 | { |
| 478 | if (empty($search)) { |
| 479 | return $subject; |
| 480 | } |
| 481 | $position = \strrpos($subject, $search); |
| 482 | if ($position === \false) { |
| 483 | return $subject; |
| 484 | } |
| 485 | return \substr($subject, 0, $position); |
| 486 | } |
| 487 | } |
| 488 |