PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / string-util.php

string-util.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/utils/string-util.php

182 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class of utilities for dealing with strings.
4 */
5
6 namespace StoreEngine\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * A class of utilities for dealing with strings.
14 */
15 final class StringUtil {
16
17 /**
18 * Checks to see whether or not a string starts with another.
19 *
20 * @param string $string The string we want to check.
21 * @param string $starts_with The string we're looking for at the start of $string.
22 * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
23 *
24 * @return bool True if the $string starts with $starts_with, false otherwise.
25 */
26 public static function starts_with( string $string, string $starts_with, bool $case_sensitive = true ): bool {
27 $len = strlen( $starts_with );
28 if ( $len > strlen( $string ) ) {
29 return false;
30 }
31
32 $string = substr( $string, 0, $len );
33
34 if ( $case_sensitive ) {
35 return strcmp( $string, $starts_with ) === 0;
36 }
37
38 return strcasecmp( $string, $starts_with ) === 0;
39 }
40
41 /**
42 * Checks to see whether or not a string ends with another.
43 *
44 * @param string $string The string we want to check.
45 * @param string $ends_with The string we're looking for at the end of $string.
46 * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
47 *
48 * @return bool True if the $string ends with $ends_with, false otherwise.
49 */
50 public static function ends_with( string $string, string $ends_with, bool $case_sensitive = true ): bool {
51 $len = strlen( $ends_with );
52 if ( $len > strlen( $string ) ) {
53 return false;
54 }
55
56 $string = substr( $string, - $len );
57
58 if ( $case_sensitive ) {
59 return strcmp( $string, $ends_with ) === 0;
60 }
61
62 return strcasecmp( $string, $ends_with ) === 0;
63 }
64
65 /**
66 * Checks if one string is contained into another at any position.
67 *
68 * @param string $string The string we want to check.
69 * @param string $contained The string we're looking for inside $string.
70 * @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
71 *
72 * @return bool True if $contained is contained inside $string, false otherwise.
73 */
74 public static function contains( string $string, string $contained, bool $case_sensitive = true ): bool {
75 if ( $case_sensitive ) {
76 return false !== strpos( $string, $contained );
77 } else {
78 return false !== stripos( $string, $contained );
79 }
80 }
81
82 /**
83 * Get the name of a plugin in the form 'directory/file.php', as in the keys of the array returned by 'get_plugins'.
84 *
85 * @param string $plugin_file_path The path of the main plugin file (can be passed as __FILE__ from the plugin itself).
86 *
87 * @return string The name of the plugin in the form 'directory/file.php'.
88 */
89 public static function plugin_name_from_plugin_file( string $plugin_file_path ): string {
90 return basename( dirname( $plugin_file_path ) ) . DIRECTORY_SEPARATOR . basename( $plugin_file_path );
91 }
92
93 /**
94 * Check if a string is null or is empty.
95 *
96 * @param string|null $value The string to check.
97 *
98 * @return bool True if the string is null or is empty.
99 */
100 public static function is_null_or_empty( ?string $value ) {
101 return is_null( $value ) || '' === $value;
102 }
103
104 /**
105 * Check if a string is null, is empty, or has only whitespace characters
106 * (space, tab, vertical tab, form feed, carriage return, new line)
107 *
108 * @param string|null $value The string to check.
109 *
110 * @return bool True if the string is null, is empty, or contains only whitespace characters.
111 */
112 public static function is_null_or_whitespace( ?string $value ): bool {
113 return is_null( $value ) || '' === $value || ctype_space( $value );
114 }
115
116 /**
117 * Convert an array of values to a list suitable for a SQL "IN" statement
118 * (so comma separated and delimited by parenthesis).
119 * e.g.: [1,2,3] --> (1,2,3)
120 *
121 * @param array $values The values to convert.
122 *
123 * @return string A parenthesized and comma-separated string generated from the values.
124 * @throws \InvalidArgumentException Empty values array passed.
125 */
126 public static function to_sql_list( array $values ) {
127 if ( empty( $values ) ) {
128 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
129 throw new \InvalidArgumentException( self::class_name_without_namespace( __CLASS__ ) . '::' . __FUNCTION__ . ': the values array is empty' );
130 }
131
132 return '(' . implode( ',', $values ) . ')';
133 }
134
135 /**
136 * Get the name of a class without the namespace.
137 *
138 * @param string $class_name The full class name.
139 *
140 * @return string The class name without the namespace.
141 */
142 public static function class_name_without_namespace( string $class_name ) {
143 // A '?:' would convert this to a one-liner, but WP coding standards disallow these :shrug:.
144 $result = substr( strrchr( $class_name, '\\' ), 1 );
145
146 return $result ? $result : $class_name;
147 }
148
149 /**
150 * Normalize the slashes (/ and \) of a local filesystem path by converting them to DIRECTORY_SEPARATOR.
151 *
152 * @param string|null $path Path to normalize.
153 *
154 * @return string|null Normalized path, or null if the input was null.
155 */
156 public static function normalize_local_path_slashes( ?string $path ) {
157 return is_null( $path ) ? null : str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path );
158 }
159
160 /**
161 * @param object|\stdClass $instance Any class instance.
162 * @param bool $esc_ns Exclude or keep namespace parts. Default true (exclude))
163 *
164 * @return string class name as slug.
165 */
166 public static function get_class_slug( $instance, bool $esc_ns = true ): string {
167 $name = str_replace( ['_', 'StoreEngine'], ['-', 'storeengine'], get_class( $instance ) );
168
169 if ( $esc_ns ) {
170 $parts = explode( '\\', $name );
171 $name = end( $parts );
172 } else {
173 $name = str_replace( '\\', '', $name );
174 }
175
176 $parts = array_filter( preg_split( '/(?=[A-Z])/', $name ) );
177
178 // Set rest base.
179 return strtolower( implode( '-', $parts ) );
180 }
181 }
182