0

Final Class Yiisoft\Hydrator\Attribute\Parameter\TrimCharacters

InheritanceYiisoft\Hydrator\Attribute\Parameter\TrimCharacters

Public Methods

Hide inherited 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\Hydrator\Attribute\Parameter\TrimCharacters
checkMultibyteFunctionsExist() Checks that all mb_* functions used by multibyte mode are available, when {@see $multibyte} is true. Yiisoft\Hydrator\Attribute\Parameter\TrimCharacters
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\Hydrator\Attribute\Parameter\TrimCharacters

Method Details

Hide inherited methods

checkDeprecatedRanges() public static method

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,
        );
    }
}

            
checkMultibyteFunctionsExist() public static method

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
}

            
expandRanges() public static method

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;
}