Skip to content

Commit

Permalink
[mbstring][PHP 8.4] Add mb_ucfirst and mb_lcfirst to polyfills
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Ayesh and zonuexe committed Mar 31, 2024
1 parent 3d71a9a commit 76bd5fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,49 @@
*/
final class Php84
{
public static function mb_ucfirst(string $string, ?string $encoding = null): string
{
if (null === $encoding) {
$encoding = mb_internal_encoding();
}

try {
$validEncoding = @mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}

// BC for PHP 7.3 and lower
if (!$validEncoding) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}

$firstChar = mb_substr($string, 0, 1, $encoding);
$firstChar = mb_convert_case($firstChar, MB_CASE_TITLE, $encoding);

return $firstChar . mb_substr($string, 1, null, $encoding);
}

public static function mb_lcfirst(string $string, ?string $encoding = null): string
{
if (null === $encoding) {
$encoding = mb_internal_encoding();
}

try {
$validEncoding = @mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}

// BC for PHP 7.3 and lower
if (!$validEncoding) {
throw new \ValueError(sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}

$firstChar = mb_substr($string, 0, 1, $encoding);
$firstChar = mb_convert_case($firstChar, MB_CASE_LOWER, $encoding);

return $firstChar . mb_substr($string, 1, null, $encoding);
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Symfony Polyfill / Php84

This component provides features added to PHP 8.4 core:

- [`mb_ucfirst` and `mb_lcfirst`](https://wiki.php.net/rfc/mb_ucfirst)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

Expand Down
9 changes: 9 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@
if (\PHP_VERSION_ID >= 80400) {
return;
}


if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
}

if (!function_exists('mb_lcfirst')) {
function mb_lcfirst($string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
}

0 comments on commit 76bd5fd

Please sign in to comment.