| @@ -1,0 +1,61 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Support\Utility; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Utility class for String manipulation. | |
| 9 | + */ | |
| 10 | +class StringUtility | |
| 11 | +{ | |
| 12 | + /** | |
| 13 | + * Convert a URL to a title. | |
| 14 | + * | |
| 15 | + * Strips the site URL from the given URL, replaces dashes with spaces, | |
| 16 | + * and capitalizes the first letter. | |
| 17 | + */ | |
| 18 | + public static function convertUrlToTitle(string $url): string | |
| 19 | + { | |
| 20 | + // Strip off the page url from the page name | |
| 21 | + $site_url = trailingslashit(get_site_url()); | |
| 22 | + $title = str_replace($site_url, '', $url); | |
| 23 | + $title = str_replace('-', ' ', $title); | |
| 24 | + | |
| 25 | + // Enforce first letter uppercase | |
| 26 | + return ucfirst($title); | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Convert a string from snake_case to PascalCase. | |
| 31 | + */ | |
| 32 | + public static function snakeToPascalCase(string $string): string | |
| 33 | + { | |
| 34 | + return str_replace('_', '', ucwords($string, '_')); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Convert a string from snake_case to camelCase. | |
| 39 | + */ | |
| 40 | + public static function snakeToCamelCase(string $string): string | |
| 41 | + { | |
| 42 | + return lcfirst(self::snakeToPascalCase($string)); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Convert a string from camelCase to snake_case. | |
| 47 | + */ | |
| 48 | + public static function camelToSnakeCase(string $string): string | |
| 49 | + { | |
| 50 | + return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string)); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Checks if the string is truly empty and not just a falsy value like '0' | |
| 55 | + * or 'false'. | |
| 56 | + */ | |
| 57 | + public static function isEmptyValue(string $string): bool | |
| 58 | + { | |
| 59 | + return empty($string) && $string !== '0'; | |
| 60 | + } | |
| 61 | +} | |