Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
92.31% |
12 / 13 |
CRAP | |
0.00% |
0 / 1 |
| StringBasic | |
95.00% |
19 / 20 |
|
92.31% |
12 / 13 |
16 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStringBasic | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| strlen | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| substr | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| strpos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| strtolower | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| strtoupper | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| strstr | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preg_match | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preg_match_all | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preg_split | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preg_replace_callback | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preg_replace | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The string wrapper class using single byte string functions. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Anatoliy Belsky <ab@php.net> |
| 12 | * @copyright 2009-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2009-04-06 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Strings; |
| 21 | |
| 22 | use phpMyFAQ\Language; |
| 23 | |
| 24 | /** |
| 25 | * Class Basic |
| 26 | * |
| 27 | * @package phpMyFAQ\Strings |
| 28 | */ |
| 29 | class StringBasic extends AbstractString |
| 30 | { |
| 31 | /** |
| 32 | * Default encoding. |
| 33 | */ |
| 34 | final public const string DEFAULT_ENCODING = 'UTF-8'; |
| 35 | |
| 36 | /** |
| 37 | * Instance. |
| 38 | */ |
| 39 | private static ?StringBasic $stringBasic = null; |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | */ |
| 44 | final private function __construct() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create and return an instance. |
| 50 | */ |
| 51 | public static function getStringBasic(string $language = 'en'): StringBasic |
| 52 | { |
| 53 | if (!self::$stringBasic instanceof StringBasic) { |
| 54 | self::$stringBasic = new self(); |
| 55 | self::$stringBasic->encoding = self::DEFAULT_ENCODING; |
| 56 | self::$stringBasic->language = Language::isASupportedLanguage($language) |
| 57 | ? $language |
| 58 | : self::DEFAULT_LANGUAGE; |
| 59 | } |
| 60 | |
| 61 | return self::$stringBasic; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get string character count. |
| 66 | * |
| 67 | * @param string $str String |
| 68 | */ |
| 69 | public static function strlen(string $str): int |
| 70 | { |
| 71 | return strlen($str); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get a part of string. |
| 76 | * |
| 77 | * @param string $str String |
| 78 | * @param int $start Start |
| 79 | * @param int|null $length Length |
| 80 | */ |
| 81 | public function substr(string $str, int $start, ?int $length = null): string |
| 82 | { |
| 83 | $length = null === $length ? strlen($str) : $length; |
| 84 | |
| 85 | return substr($str, $start, $length); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get position of the first occurrence of a string. |
| 90 | * |
| 91 | * @param string $haystack Haystack |
| 92 | * @param string $needle Needle |
| 93 | * @param int $offset Offset |
| 94 | */ |
| 95 | public function strpos(string $haystack, string $needle, int $offset = 0): int|false |
| 96 | { |
| 97 | return strpos($haystack, $needle, $offset); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Make a string lower case. |
| 102 | * |
| 103 | * @param string $str String |
| 104 | */ |
| 105 | public function strtolower(string $str): string |
| 106 | { |
| 107 | return strtolower($str); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Make a string upper case. |
| 112 | * |
| 113 | * @param string $str String |
| 114 | */ |
| 115 | public function strtoupper(string $str): string |
| 116 | { |
| 117 | return strtoupper($str); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get occurrence of a string within another. |
| 122 | * |
| 123 | * @param string $haystack Haystack |
| 124 | * @param string $needle Needle |
| 125 | * @param bool $part Part |
| 126 | */ |
| 127 | public function strstr(string $haystack, string $needle, bool $part = false): string|false |
| 128 | { |
| 129 | return strstr($haystack, $needle, $part); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Match a regexp. |
| 134 | * |
| 135 | * @return int|false |
| 136 | */ |
| 137 | /** |
| 138 | * @param array<array-key, mixed>|null $matches |
| 139 | */ |
| 140 | public function preg_match( |
| 141 | string $pattern, |
| 142 | string $subject, |
| 143 | &$matches = null, |
| 144 | int $flags = 0, |
| 145 | int $offset = 0, |
| 146 | ): int|false { /* phpcs:ignore */ |
| 147 | return preg_match($pattern, $subject, $matches, $flags, $offset); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Match a regexp globally. |
| 152 | * |
| 153 | * @param array<array-key, mixed>|null $matches |
| 154 | * @return int|false |
| 155 | */ |
| 156 | /** |
| 157 | * @param array<array-key, mixed>|null $matches |
| 158 | */ |
| 159 | public function preg_match_all( |
| 160 | string $pattern, |
| 161 | string $subject, |
| 162 | &$matches = null, |
| 163 | int $flags = 0, |
| 164 | int $offset = 0, |
| 165 | ): int|false { /* phpcs:ignore */ |
| 166 | return preg_match_all($pattern, $subject, $matches, $flags, $offset); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Split string by a regexp. |
| 171 | */ |
| 172 | public function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|bool |
| 173 | { |
| 174 | return preg_split($pattern, $subject, $limit, $flags); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Search and replace by a regexp using a callback. |
| 179 | * |
| 180 | * @param string|string[] $pattern |
| 181 | * @param callable(array<array-key, string>): string $callback |
| 182 | * @param string|string[] $subject |
| 183 | * @return string|string[]|null |
| 184 | */ |
| 185 | public function preg_replace_callback( |
| 186 | string|array $pattern, |
| 187 | callable $callback, |
| 188 | string|array $subject, |
| 189 | int $limit = -1, |
| 190 | int &$count = 0, |
| 191 | ): string|array|null { |
| 192 | return preg_replace_callback($pattern, $callback, $subject, $limit, $count); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Search and replace by a regexp. |
| 197 | * |
| 198 | * @param string|string[] $pattern |
| 199 | * @param string|string[] $replacement |
| 200 | * @param string|string[] $subject |
| 201 | * @return string|string[]|null |
| 202 | */ |
| 203 | public function preg_replace( |
| 204 | string|array $pattern, |
| 205 | string|array $replacement, |
| 206 | string|array $subject, |
| 207 | int $limit = -1, |
| 208 | int &$count = 0, |
| 209 | ): string|array|null { |
| 210 | return preg_replace($pattern, $replacement, $subject, $limit, $count); |
| 211 | } |
| 212 | } |