Skip to content
Merged

Dev #15

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"autoload": {
"psr-4": {
"AlfacodeTeam\\PhpIoCli\\": "src/"
}
},
"files": [
"src/mb_polyfill.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class AbstractCommand

private bool $rethrowExceptions = false;

final public function __construct()
public function __construct()
{
$this->configure();

Expand Down
51 changes: 51 additions & 0 deletions src/Internal/MbTrim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace AlfacodeTeam\PhpIoCli\Internal;

/**
* Backing implementation for the mb_trim/mb_ltrim/mb_rtrim polyfills (PHP < 8.4).
*
* @internal
*/
final class MbTrim
{
/** Default whitespace set, matching the native PHP 8.4 mb_trim behaviour. */
private const DEFAULT = " \f\n\r\t\x0B\x00";

public static function run(string $string, ?string $characters, bool $left, bool $right): string
{
$characters ??= self::DEFAULT;

if ($characters === '') {
return $string;
}

// Split the character list into individual (possibly multibyte) characters.
$chars = preg_split('//u', $characters, -1, PREG_SPLIT_NO_EMPTY);
if ($chars === false || $chars === []) {
return $string;
}

$class = '';
foreach ($chars as $char) {
$class .= preg_quote($char, '/');
}

$pattern = '';
if ($left) {
$pattern .= '^[' . $class . ']+';
}
if ($left && $right) {
$pattern .= '|';
}
if ($right) {
$pattern .= '[' . $class . ']+$';
}

$result = preg_replace('/' . $pattern . '/u', '', $string);

return $result ?? $string;
}
}
36 changes: 36 additions & 0 deletions src/mb_polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* Polyfills for the multibyte trim functions introduced in PHP 8.4.
* This library targets PHP 8.2+, so on PHP < 8.4 these functions are absent.
* The implementations mirror the native behaviour: when no character list is
* supplied they strip the same default whitespace set as the native functions.
*/

if (!function_exists('mb_trim')) {
/**
* @param string $string
* @param string|null $characters
* @param string|null $encoding
*/
function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string
{
return \AlfacodeTeam\PhpIoCli\Internal\MbTrim::run($string, $characters, true, true);
}
}

if (!function_exists('mb_ltrim')) {
function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string
{
return \AlfacodeTeam\PhpIoCli\Internal\MbTrim::run($string, $characters, true, false);
}
}

if (!function_exists('mb_rtrim')) {
function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string
{
return \AlfacodeTeam\PhpIoCli\Internal\MbTrim::run($string, $characters, false, true);
}
}
Loading