ArrayUtil.php
1 year ago
CallbackUtil.php
5 months ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
5 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
MetaDataUtil.php
2 months ago
NumberUtil.php
11 months ago
OrderUtil.php
7 months ago
PluginUtil.php
4 weeks ago
RestApiUtil.php
7 months ago
ShippingUtil.php
1 year ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
StringUtil.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A class of utilities for dealing with strings. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Utilities; |
| 7 | |
| 8 | /** |
| 9 | * A class of utilities for dealing with strings. |
| 10 | */ |
| 11 | final class StringUtil { |
| 12 | |
| 13 | /** |
| 14 | * Checks to see whether or not a string starts with another. |
| 15 | * |
| 16 | * @param string $string The string we want to check. |
| 17 | * @param string $starts_with The string we're looking for at the start of $string. |
| 18 | * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive. |
| 19 | * |
| 20 | * @return bool True if the $string starts with $starts_with, false otherwise. |
| 21 | */ |
| 22 | public static function starts_with( string $string, string $starts_with, bool $case_sensitive = true ): bool { |
| 23 | $len = strlen( $starts_with ); |
| 24 | if ( $len > strlen( $string ) ) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | $string = substr( $string, 0, $len ); |
| 29 | |
| 30 | if ( $case_sensitive ) { |
| 31 | return strcmp( $string, $starts_with ) === 0; |
| 32 | } |
| 33 | |
| 34 | return strcasecmp( $string, $starts_with ) === 0; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Checks to see whether or not a string ends with another. |
| 39 | * |
| 40 | * @param string $string The string we want to check. |
| 41 | * @param string $ends_with The string we're looking for at the end of $string. |
| 42 | * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive. |
| 43 | * |
| 44 | * @return bool True if the $string ends with $ends_with, false otherwise. |
| 45 | */ |
| 46 | public static function ends_with( string $string, string $ends_with, bool $case_sensitive = true ): bool { |
| 47 | $len = strlen( $ends_with ); |
| 48 | if ( $len > strlen( $string ) ) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $string = substr( $string, -$len ); |
| 53 | |
| 54 | if ( $case_sensitive ) { |
| 55 | return strcmp( $string, $ends_with ) === 0; |
| 56 | } |
| 57 | |
| 58 | return strcasecmp( $string, $ends_with ) === 0; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Checks if one string is contained into another at any position. |
| 63 | * |
| 64 | * @param string $string The string we want to check. |
| 65 | * @param string $contained The string we're looking for inside $string. |
| 66 | * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive. |
| 67 | * @return bool True if $contained is contained inside $string, false otherwise. |
| 68 | */ |
| 69 | public static function contains( string $string, string $contained, bool $case_sensitive = true ): bool { |
| 70 | if ( $case_sensitive ) { |
| 71 | return false !== strpos( $string, $contained ); |
| 72 | } else { |
| 73 | return false !== stripos( $string, $contained ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the name of a plugin in the form 'directory/file.php', as in the keys of the array returned by 'get_plugins'. |
| 79 | * |
| 80 | * @param string $plugin_file_path The path of the main plugin file (can be passed as __FILE__ from the plugin itself). |
| 81 | * @return string The name of the plugin in the form 'directory/file.php'. |
| 82 | */ |
| 83 | public static function plugin_name_from_plugin_file( string $plugin_file_path ): string { |
| 84 | return basename( dirname( $plugin_file_path ) ) . DIRECTORY_SEPARATOR . basename( $plugin_file_path ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Check if a string is null or is empty. |
| 89 | * |
| 90 | * @param string|null $value The string to check. |
| 91 | * @return bool True if the string is null or is empty. |
| 92 | */ |
| 93 | public static function is_null_or_empty( ?string $value ) { |
| 94 | return is_null( $value ) || '' === $value; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if a string is null, is empty, or has only whitespace characters |
| 99 | * (space, tab, vertical tab, form feed, carriage return, new line) |
| 100 | * |
| 101 | * @param string|null $value The string to check. |
| 102 | * @return bool True if the string is null, is empty, or contains only whitespace characters. |
| 103 | */ |
| 104 | public static function is_null_or_whitespace( ?string $value ) { |
| 105 | return is_null( $value ) || '' === $value || ctype_space( $value ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Convert an array of values to a list suitable for a SQL "IN" statement |
| 110 | * (so comma separated and delimited by parenthesis). |
| 111 | * e.g.: [1,2,3] --> (1,2,3) |
| 112 | * |
| 113 | * @param array $values The values to convert. |
| 114 | * @return string A parenthesized and comma-separated string generated from the values. |
| 115 | * @throws \InvalidArgumentException Empty values array passed. |
| 116 | */ |
| 117 | public static function to_sql_list( array $values ) { |
| 118 | if ( empty( $values ) ) { |
| 119 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 120 | throw new \InvalidArgumentException( self::class_name_without_namespace( __CLASS__ ) . '::' . __FUNCTION__ . ': the values array is empty' ); |
| 121 | } |
| 122 | |
| 123 | return '(' . implode( ',', $values ) . ')'; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the name of a class without the namespace. |
| 128 | * |
| 129 | * @param string $class_name The full class name. |
| 130 | * @return string The class name without the namespace. |
| 131 | */ |
| 132 | public static function class_name_without_namespace( string $class_name ) { |
| 133 | // A '?:' would convert this to a one-liner, but WP coding standards disallow these :shrug:. |
| 134 | $result = substr( strrchr( $class_name, '\\' ), 1 ); |
| 135 | return $result ? $result : $class_name; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Normalize the slashes (/ and \) of a local filesystem path by converting them to DIRECTORY_SEPARATOR. |
| 140 | * |
| 141 | * @param string|null $path Path to normalize. |
| 142 | * @return string|null Normalized path, or null if the input was null. |
| 143 | */ |
| 144 | public static function normalize_local_path_slashes( ?string $path ) { |
| 145 | return is_null( $path ) ? null : str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path ); |
| 146 | } |
| 147 | } |
| 148 |