Skip to content

Commit 76bd5fd

Browse files
Ayeshzonuexe
andcommitted
[mbstring][PHP 8.4] Add mb_ucfirst and mb_lcfirst to polyfills
Adds polyfills for `mb_ucfirst` and `mb_lcfirst` functions based on the polyfill shown in PHP.Watch. It basically takes the first mb character, calls `mb_convert_case` with `MB_CASE_TITLE` or `MB_CASE_LOWER`, and returns with the concat of the remaining string. The tests are taken from the php-src PR. - [RFC](https://wiki.php.net/rfc/mb_ucfirst) - [php-src PR: php-src#13161](php/php-src#13161) - [PHP.Watch - PHP 8.4: New `mb_ucfirst` and `mb_lcfirst` functions](https://php.watch/versions/8.4/mb_ucfirst-mb_ucfirst) Co-authored-by: USAMI Kenta <tadsan@zonu.me>
1 parent 3d71a9a commit 76bd5fd

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

Php84.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,49 @@
1818
*/
1919
final class Php84
2020
{
21+
public static function mb_ucfirst(string $string, ?string $encoding = null): string
22+
{
23+
if (null === $encoding) {
24+
$encoding = mb_internal_encoding();
25+
}
26+
27+
try {
28+
$validEncoding = @mb_check_encoding('', $encoding);
29+
} catch (\ValueError $e) {
30+
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
31+
}
32+
33+
// BC for PHP 7.3 and lower
34+
if (!$validEncoding) {
35+
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
36+
}
37+
38+
$firstChar = mb_substr($string, 0, 1, $encoding);
39+
$firstChar = mb_convert_case($firstChar, MB_CASE_TITLE, $encoding);
40+
41+
return $firstChar . mb_substr($string, 1, null, $encoding);
42+
}
43+
44+
public static function mb_lcfirst(string $string, ?string $encoding = null): string
45+
{
46+
if (null === $encoding) {
47+
$encoding = mb_internal_encoding();
48+
}
49+
50+
try {
51+
$validEncoding = @mb_check_encoding('', $encoding);
52+
} catch (\ValueError $e) {
53+
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
54+
}
55+
56+
// BC for PHP 7.3 and lower
57+
if (!$validEncoding) {
58+
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
59+
}
60+
61+
$firstChar = mb_substr($string, 0, 1, $encoding);
62+
$firstChar = mb_convert_case($firstChar, MB_CASE_LOWER, $encoding);
63+
64+
return $firstChar . mb_substr($string, 1, null, $encoding);
65+
}
2166
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Symfony Polyfill / Php84
33

44
This component provides features added to PHP 8.4 core:
55

6+
- [`mb_ucfirst` and `mb_lcfirst`](https://wiki.php.net/rfc/mb_ucfirst)
7+
68
More information can be found in the
79
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
810

bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
1414
if (\PHP_VERSION_ID >= 80400) {
1515
return;
1616
}
17+
18+
19+
if (!function_exists('mb_ucfirst')) {
20+
function mb_ucfirst($string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
21+
}
22+
23+
if (!function_exists('mb_lcfirst')) {
24+
function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
25+
}

0 commit comments

Comments
 (0)