We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
2 | /** |
||
3 | * This file is part of PHP-Typography. |
||
4 | * |
||
5 | * Copyright 2014-2020 Peter Putzer. |
||
6 | * Copyright 2009-2011 KINGdesk, LLC. |
||
7 | * |
||
8 | * This program is free software; you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation; either version 2 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License along |
||
19 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
21 | * |
||
22 | * *** |
||
23 | * |
||
24 | * @package mundschenk-at/php-typography |
||
25 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
||
26 | */ |
||
27 | |||
28 | namespace PHP_Typography; |
||
29 | |||
30 | /** |
||
31 | * A utility class to handle fast and save string function access. |
||
32 | * |
||
33 | * @since 4.2.0 |
||
34 | */ |
||
35 | abstract class Strings { |
||
36 | /** |
||
37 | * Utility patterns for splitting string parameter lists into arrays. |
||
38 | * |
||
39 | * @internal |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | const RE_PARAMETER_SPLITTING = '/[\s,]+/S'; |
||
44 | |||
45 | /** |
||
46 | * An array of encodings in detection order. |
||
47 | * |
||
48 | * ASCII has to be first to have a chance of detection. |
||
49 | * |
||
50 | * @internal |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | const ENCODINGS = [ 'ASCII', 'UTF-8' ]; |
||
55 | |||
56 | /** |
||
57 | * A hash map for string functions according to encoding. |
||
58 | * |
||
59 | * @internal |
||
60 | * |
||
61 | * @var array $encoding => [ 'strlen' => $function_name, ... ]. |
||
62 | */ |
||
63 | const STRING_FUNCTIONS = [ |
||
64 | 'UTF-8' => [ |
||
65 | 'strlen' => 'mb_strlen', |
||
66 | 'str_split' => [ __CLASS__, 'mb_str_split' ], |
||
67 | 'strtolower' => 'mb_strtolower', |
||
68 | 'strtoupper' => 'mb_strtoupper', |
||
69 | 'substr' => 'mb_substr', |
||
70 | 'u' => 'u', |
||
71 | ], |
||
72 | 'ASCII' => [ |
||
73 | 'strlen' => 'strlen', |
||
74 | 'str_split' => 'str_split', |
||
75 | 'strtolower' => 'strtolower', |
||
76 | 'strtoupper' => 'strtoupper', |
||
77 | 'substr' => 'substr', |
||
78 | 'u' => '', |
||
79 | ], |
||
80 | false => [], |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Retrieves str* functions. |
||
85 | * |
||
86 | * @param string $str A string to detect the encoding from. |
||
87 | * @return array { |
||
88 | * An array of string functions. |
||
89 | * |
||
90 | * 'strlen' => callable, |
||
91 | * 'str_split' => callable, |
||
92 | * 'strtolower' => callable, |
||
93 | * 'strtoupper' => callable, |
||
94 | * 'substr' => callable, |
||
95 | * 'u' => modifier string |
||
96 | * } |
||
97 | */ |
||
98 | 1 | public static function functions( $str ) { |
|
99 | 1 | return self::STRING_FUNCTIONS[ \mb_detect_encoding( $str, self::ENCODINGS, true ) ]; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Multibyte-safe str_split function. Unlike regular str_split, behavior for |
||
104 | * `$split_length` < 1 is undefined and may or may not result in an error |
||
105 | * being raised. |
||
106 | * |
||
107 | * @param string $string The input string. |
||
108 | * @param int $split_length Optional. Maximum length of the chunk. Default 1. |
||
109 | * |
||
110 | * @return string[] An array of $split_length character chunks. |
||
111 | */ |
||
112 | 6 | public static function mb_str_split( $string, $split_length = 1 ) { |
|
113 | // Checking here is not optimal, the check should be made on instantiation |
||
114 | // when the class is refactored. |
||
115 | 6 | if ( \function_exists( 'mb_str_split' ) ) { |
|
116 | // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.mb_str_splitFound |
||
117 | 6 | return (array) \mb_str_split( $string, $split_length, 'UTF-8' ); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
118 | } |
||
119 | |||
120 | // We can safely cast to an array here, as long as $string convertible to a string. |
||
121 | return (array) \preg_split( "/(.{{$split_length}})/us", $string , -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE ); |
||
0 ignored issues
–
show
|
|||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Converts decimal value to unicode character. |
||
126 | * |
||
127 | * @param int|string|array $codes Decimal value(s) coresponding to unicode character(s). |
||
128 | * |
||
129 | * @return string Unicode character(s). |
||
130 | */ |
||
131 | 10 | public static function uchr( $codes ) { |
|
132 | |||
133 | // Single character code. |
||
134 | 10 | if ( \is_scalar( $codes ) ) { |
|
135 | 10 | $codes = \func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
|
136 | } |
||
137 | |||
138 | // Deal with an array of character codes. |
||
139 | 10 | $json = '"'; |
|
140 | 10 | foreach ( $codes as $code ) { |
|
141 | 10 | $json .= \sprintf( '\u%04x', $code ); |
|
142 | } |
||
143 | 10 | $json .= '"'; |
|
144 | |||
145 | 10 | return \json_decode( $json ); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * If necessary, split the passed parameters string into an array. |
||
150 | * |
||
151 | * @param array|string $params Parameters. |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | 7 | public static function maybe_split_parameters( $params ) { |
|
156 | 7 | if ( ! \is_array( $params ) ) { |
|
157 | // We can safely cast to an array here, as long as $params convertible to a string. |
||
158 | 4 | $params = (array) \preg_split( self::RE_PARAMETER_SPLITTING, $params, -1, PREG_SPLIT_NO_EMPTY ); |
|
159 | } |
||
160 | |||
161 | 7 | return $params; |
|
162 | } |
||
163 | } |
||
164 |