Final Class Yiisoft\Hydrator\Attribute\Parameter\TrimCharacters
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| checkDeprecatedRanges() | Triggers a deprecation notice when {@see $characters} contain the .. range syntax. In multibyte mode,
{@see $characters} are treated as a string in the {@see $encoding}. |
Yiisoft\ |
| checkMultibyteFunctionsExist() | Checks that all mb_* functions used by multibyte mode are available, when {@see $multibyte} is true. |
Yiisoft\ |
| expandRanges() | Expands .. ranges in {@see $characters} into an explicit, range-free character list, mirroring the
range parsing of native {@see trim()} (see php_charmask() in ext/standard/string.c), including its
E_WARNING messages and literal fallback for malformed ranges. |
Yiisoft\ |
Method Details
Triggers a deprecation notice when {@see $characters} contain the .. range syntax. In multibyte mode,
{@see $characters} are treated as a string in the {@see $encoding}.
| public static void checkDeprecatedRanges ( ?string $characters, boolean $multibyte, ?string $encoding ) | ||
| $characters | ?string | |
| $multibyte | boolean | |
| $encoding | ?string | |
public static function checkDeprecatedRanges(?string $characters, bool $multibyte, ?string $encoding): void
{
if ($characters === null) {
return;
}
$hasRanges = $multibyte
? mb_substr_count($characters, str_repeat(self::dot($encoding), 2), $encoding) > 0
: str_contains($characters, '..');
if ($hasRanges) {
trigger_error(
'The ".." range syntax of the "characters" parameter is deprecated and will be removed in the next major version.',
E_USER_DEPRECATED,
);
}
}
Checks that all mb_* functions used by multibyte mode are available, when {@see $multibyte} is true.
| public static void checkMultibyteFunctionsExist ( ?bool $multibyte ) | ||
| $multibyte | ?bool | |
public static function checkMultibyteFunctionsExist(?bool $multibyte): void
{
if ($multibyte !== true) {
return;
}
self::$multibyteFunctionsExist ??= function_exists('mb_trim')
&& function_exists('mb_ltrim')
&& function_exists('mb_rtrim')
&& function_exists('mb_str_split')
&& function_exists('mb_substr_count')
&& function_exists('mb_ord')
&& function_exists('mb_chr')
&& function_exists('mb_convert_encoding')
&& function_exists('mb_internal_encoding');
if (self::$multibyteFunctionsExist) {
return;
}
// @codeCoverageIgnoreStart
throw new LogicException(
'The "multibyte" parameter requires "mbstring" extension since PHP 8.4 or "symfony/polyfill-mbstring"'
. ' package on earlier versions.',
);
// @codeCoverageIgnoreEnd
}
Expands .. ranges in {@see $characters} into an explicit, range-free character list, mirroring the
range parsing of native {@see trim()} (see php_charmask() in ext/standard/string.c), including its
E_WARNING messages and literal fallback for malformed ranges.
| public static string expandRanges ( string $characters, ?string $encoding ) | ||
| $characters | string | |
| $encoding | ?string | |
public static function expandRanges(string $characters, ?string $encoding): string
{
$encoding ??= mb_internal_encoding();
/** @var list<string> $chars */
$chars = mb_str_split($characters, 1, $encoding);
$count = count($chars);
$dot = self::dot($encoding);
$native = self::supportsCodePointFunctions($encoding);
$result = '';
for ($i = 0; $i < $count; $i++) {
$char = $chars[$i];
if ($i + 3 < $count && $chars[$i + 1] === $dot && $chars[$i + 2] === $dot) {
$startOrd = self::ord($char, $encoding, $native);
$endOrd = self::ord($chars[$i + 3], $encoding, $native);
if ($startOrd !== false && $endOrd !== false && $endOrd >= $startOrd) {
for ($ord = $startOrd; $ord <= $endOrd; $ord++) {
$rangeChar = self::chr($ord, $encoding, $native);
if ($rangeChar !== false) {
$result .= $rangeChar;
}
}
$i += 3;
continue;
}
}
if ($i + 1 < $count && $char === $dot && $chars[$i + 1] === $dot) {
if ($i === 0) {
trigger_error("Invalid '..'-range, no character to the left of '..'", E_USER_WARNING);
continue;
}
if ($i + 2 >= $count) {
trigger_error("Invalid '..'-range, no character to the right of '..'", E_USER_WARNING);
continue;
}
$leftOrd = self::ord($chars[$i - 1], $encoding, $native);
$rightOrd = self::ord($chars[$i + 2], $encoding, $native);
if ($leftOrd !== false && $rightOrd !== false && $leftOrd > $rightOrd) {
trigger_error("Invalid '..'-range, '..'-range needs to be incrementing", E_USER_WARNING);
continue;
}
trigger_error("Invalid '..'-range", E_USER_WARNING);
continue;
}
$result .= $char;
}
return $result;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.