Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractString.php

class AbstractString

Represents a string of abstract characters.

Unicode defines 3 types of "characters" (bytes, code points and grapheme clusters). This class is the abstract type to use as a type-hint when the logic you want to implement doesn't care about the exact variant it deals with.

@author Nicolas Grekas <p@tchwork.com> @author Hugo Hamon <hugohamon@neuf.fr>

Hierarchy

  • class \Symfony\Component\String\AbstractString implements \Symfony\Component\String\Stringable, \Symfony\Component\String\JsonSerializable

Expanded class hierarchy of AbstractString

File

vendor/symfony/string/AbstractString.php, line 30

Namespace

Symfony\Component\String
View source
abstract class AbstractString implements \Stringable, \JsonSerializable {
    public const PREG_PATTERN_ORDER = \PREG_PATTERN_ORDER;
    public const PREG_SET_ORDER = \PREG_SET_ORDER;
    public const PREG_OFFSET_CAPTURE = \PREG_OFFSET_CAPTURE;
    public const PREG_UNMATCHED_AS_NULL = \PREG_UNMATCHED_AS_NULL;
    public const PREG_SPLIT = 0;
    public const PREG_SPLIT_NO_EMPTY = \PREG_SPLIT_NO_EMPTY;
    public const PREG_SPLIT_DELIM_CAPTURE = \PREG_SPLIT_DELIM_CAPTURE;
    public const PREG_SPLIT_OFFSET_CAPTURE = \PREG_SPLIT_OFFSET_CAPTURE;
    protected string $string = '';
    protected ?bool $ignoreCase = false;
    public abstract function __construct(string $string = '');
    
    /**
     * Unwraps instances of AbstractString back to strings.
     *
     * @return string[]|array
     */
    public static function unwrap(array $values) : array {
        foreach ($values as $k => $v) {
            if ($v instanceof self) {
                $values[$k] = $v->__toString();
            }
            elseif (\is_array($v) && $values[$k] !== ($v = static::unwrap($v))) {
                $values[$k] = $v;
            }
        }
        return $values;
    }
    
    /**
     * Wraps (and normalizes) strings in instances of AbstractString.
     *
     * @return static[]|array
     */
    public static function wrap(array $values) : array {
        $i = 0;
        $keys = null;
        foreach ($values as $k => $v) {
            if (\is_string($k) && '' !== $k && $k !== ($j = (string) new static($k))) {
                $keys ??= array_keys($values);
                $keys[$i] = $j;
            }
            if (\is_string($v)) {
                $values[$k] = new static($v);
            }
            elseif (\is_array($v) && $values[$k] !== ($v = static::wrap($v))) {
                $values[$k] = $v;
            }
            ++$i;
        }
        return null !== $keys ? array_combine($keys, $values) : $values;
    }
    
    /**
     * @param string|string[] $needle
     */
    public function after(string|iterable $needle, bool $includeNeedle = false, int $offset = 0) : static {
        $str = clone $this;
        $i = \PHP_INT_MAX;
        if (\is_string($needle)) {
            $needle = [
                $needle,
            ];
        }
        foreach ($needle as $n) {
            $n = (string) $n;
            $j = $this->indexOf($n, $offset);
            if (null !== $j && $j < $i) {
                $i = $j;
                $str->string = $n;
            }
        }
        if (\PHP_INT_MAX === $i) {
            return $str;
        }
        if (!$includeNeedle) {
            $i += $str->length();
        }
        return $this->slice($i);
    }
    
    /**
     * @param string|string[] $needle
     */
    public function afterLast(string|iterable $needle, bool $includeNeedle = false, int $offset = 0) : static {
        $str = clone $this;
        $i = null;
        if (\is_string($needle)) {
            $needle = [
                $needle,
            ];
        }
        foreach ($needle as $n) {
            $n = (string) $n;
            $j = $this->indexOfLast($n, $offset);
            if (null !== $j && $j >= $i) {
                $i = $offset = $j;
                $str->string = $n;
            }
        }
        if (null === $i) {
            return $str;
        }
        if (!$includeNeedle) {
            $i += $str->length();
        }
        return $this->slice($i);
    }
    public abstract function append(string ...$suffix) : static;
    
    /**
     * @param string|string[] $needle
     */
    public function before(string|iterable $needle, bool $includeNeedle = false, int $offset = 0) : static {
        $str = clone $this;
        $i = \PHP_INT_MAX;
        if (\is_string($needle)) {
            $needle = [
                $needle,
            ];
        }
        foreach ($needle as $n) {
            $n = (string) $n;
            $j = $this->indexOf($n, $offset);
            if (null !== $j && $j < $i) {
                $i = $j;
                $str->string = $n;
            }
        }
        if (\PHP_INT_MAX === $i) {
            return $str;
        }
        if ($includeNeedle) {
            $i += $str->length();
        }
        return $this->slice(0, $i);
    }
    
    /**
     * @param string|string[] $needle
     */
    public function beforeLast(string|iterable $needle, bool $includeNeedle = false, int $offset = 0) : static {
        $str = clone $this;
        $i = null;
        if (\is_string($needle)) {
            $needle = [
                $needle,
            ];
        }
        foreach ($needle as $n) {
            $n = (string) $n;
            $j = $this->indexOfLast($n, $offset);
            if (null !== $j && $j >= $i) {
                $i = $offset = $j;
                $str->string = $n;
            }
        }
        if (null === $i) {
            return $str;
        }
        if ($includeNeedle) {
            $i += $str->length();
        }
        return $this->slice(0, $i);
    }
    
    /**
     * @return int[]
     */
    public function bytesAt(int $offset) : array {
        $str = $this->slice($offset, 1);
        return '' === $str->string ? [] : array_values(unpack('C*', $str->string));
    }
    public abstract function camel() : static;
    
    /**
     * @return static[]
     */
    public abstract function chunk(int $length = 1) : array;
    public function collapseWhitespace() : static {
        $str = clone $this;
        $str->string = trim(preg_replace("/(?:[ \n\r\t\f]{2,}+|[\n\r\t\f])/", ' ', $str->string), " \n\r\t\f");
        return $str;
    }
    
    /**
     * @param string|string[] $needle
     */
    public function containsAny(string|iterable $needle) : bool {
        return null !== $this->indexOf($needle);
    }
    
    /**
     * @param string|string[] $suffix
     */
    public function endsWith(string|iterable $suffix) : bool {
        if (\is_string($suffix)) {
            throw new \TypeError(\sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
        }
        foreach ($suffix as $s) {
            if ($this->endsWith((string) $s)) {
                return true;
            }
        }
        return false;
    }
    public function ensureEnd(string $suffix) : static {
        if (!$this->endsWith($suffix)) {
            return $this->append($suffix);
        }
        $suffix = preg_quote($suffix);
        $regex = '{(' . $suffix . ')(?:' . $suffix . ')++$}D';
        return $this->replaceMatches($regex . ($this->ignoreCase ? 'i' : ''), '$1');
    }
    public function ensureStart(string $prefix) : static {
        $prefix = new static($prefix);
        if (!$this->startsWith($prefix)) {
            return $this->prepend($prefix);
        }
        $str = clone $this;
        $i = $prefixLen = $prefix->length();
        while ($this->indexOf($prefix, $i) === $i) {
            $str = $str->slice($prefixLen);
            $i += $prefixLen;
        }
        return $str;
    }
    
    /**
     * @param string|string[] $string
     */
    public function equalsTo(string|iterable $string) : bool {
        if (\is_string($string)) {
            throw new \TypeError(\sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
        }
        foreach ($string as $s) {
            if ($this->equalsTo((string) $s)) {
                return true;
            }
        }
        return false;
    }
    public abstract function folded() : static;
    public function ignoreCase() : static {
        $str = clone $this;
        $str->ignoreCase = true;
        return $str;
    }
    
    /**
     * @param string|string[] $needle
     */
    public function indexOf(string|iterable $needle, int $offset = 0) : ?int {
        if (\is_string($needle)) {
            throw new \TypeError(\sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
        }
        $i = \PHP_INT_MAX;
        foreach ($needle as $n) {
            $j = $this->indexOf((string) $n, $offset);
            if (null !== $j && $j < $i) {
                $i = $j;
            }
        }
        return \PHP_INT_MAX === $i ? null : $i;
    }
    
    /**
     * @param string|string[] $needle
     */
    public function indexOfLast(string|iterable $needle, int $offset = 0) : ?int {
        if (\is_string($needle)) {
            throw new \TypeError(\sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
        }
        $i = null;
        foreach ($needle as $n) {
            $j = $this->indexOfLast((string) $n, $offset);
            if (null !== $j && $j >= $i) {
                $i = $offset = $j;
            }
        }
        return $i;
    }
    public function isEmpty() : bool {
        return '' === $this->string;
    }
    public abstract function join(array $strings, ?string $lastGlue = null) : static;
    public function jsonSerialize() : string {
        return $this->string;
    }
    public abstract function length() : int;
    public abstract function lower() : static;
    
    /**
     * Matches the string using a regular expression.
     *
     * Pass PREG_PATTERN_ORDER or PREG_SET_ORDER as $flags to get all occurrences matching the regular expression.
     *
     * @return array All matches in a multi-dimensional array ordered according to flags
     */
    public abstract function match(string $regexp, int $flags = 0, int $offset = 0) : array;
    public abstract function padBoth(int $length, string $padStr = ' ') : static;
    public abstract function padEnd(int $length, string $padStr = ' ') : static;
    public abstract function padStart(int $length, string $padStr = ' ') : static;
    public abstract function prepend(string ...$prefix) : static;
    public function repeat(int $multiplier) : static {
        if (0 > $multiplier) {
            throw new InvalidArgumentException(\sprintf('Multiplier must be positive, %d given.', $multiplier));
        }
        $str = clone $this;
        $str->string = str_repeat($str->string, $multiplier);
        return $str;
    }
    public abstract function replace(string $from, string $to) : static;
    public abstract function replaceMatches(string $fromRegexp, string|callable $to) : static;
    public abstract function reverse() : static;
    public abstract function slice(int $start = 0, ?int $length = null) : static;
    public abstract function snake() : static;
    public function kebab() : static {
        return $this->snake()
            ->replace('_', '-');
    }
    public abstract function splice(string $replacement, int $start = 0, ?int $length = null) : static;
    
    /**
     * @return static[]
     */
    public function split(string $delimiter, ?int $limit = null, ?int $flags = null) : array {
        if (null === $flags) {
            throw new \TypeError('Split behavior when $flags is null must be implemented by child classes.');
        }
        if ($this->ignoreCase) {
            $delimiter .= 'i';
        }
        set_error_handler(static fn($t, $m) => throw new InvalidArgumentException($m));
        try {
            if (false === ($chunks = preg_split($delimiter, $this->string, $limit, $flags))) {
                throw new RuntimeException('Splitting failed with error: ' . preg_last_error_msg());
            }
        } finally {
            restore_error_handler();
        }
        $str = clone $this;
        if (self::PREG_SPLIT_OFFSET_CAPTURE & $flags) {
            foreach ($chunks as &$chunk) {
                $str->string = $chunk[0];
                $chunk[0] = clone $str;
            }
        }
        else {
            foreach ($chunks as &$chunk) {
                $str->string = $chunk;
                $chunk = clone $str;
            }
        }
        return $chunks;
    }
    
    /**
     * @param string|string[] $prefix
     */
    public function startsWith(string|iterable $prefix) : bool {
        if (\is_string($prefix)) {
            throw new \TypeError(\sprintf('Method "%s()" must be overridden by class "%s" to deal with non-iterable values.', __FUNCTION__, static::class));
        }
        foreach ($prefix as $prefix) {
            if ($this->startsWith((string) $prefix)) {
                return true;
            }
        }
        return false;
    }
    public abstract function title(bool $allWords = false) : static;
    public function toByteString(?string $toEncoding = null) : ByteString {
        $b = new ByteString();
        $toEncoding = \in_array($toEncoding, [
            'utf8',
            'utf-8',
            'UTF8',
        ], true) ? 'UTF-8' : $toEncoding;
        if (null === $toEncoding || $toEncoding === ($fromEncoding = $this instanceof AbstractUnicodeString || preg_match('//u', $b->string) ? 'UTF-8' : 'Windows-1252')) {
            $b->string = $this->string;
            return $b;
        }
        try {
            $b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
        } catch (\ValueError $e) {
            if (!\function_exists('iconv')) {
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
            }
            $b->string = iconv('UTF-8', $toEncoding, $this->string);
        }
        return $b;
    }
    public function toCodePointString() : CodePointString {
        return new CodePointString($this->string);
    }
    public function toString() : string {
        return $this->string;
    }
    public function toUnicodeString() : UnicodeString {
        return new UnicodeString($this->string);
    }
    public abstract function trim(string $chars = " \t\n\r\x00\v\f ") : static;
    public abstract function trimEnd(string $chars = " \t\n\r\x00\v\f ") : static;
    
    /**
     * @param string|string[] $prefix
     */
    public function trimPrefix($prefix) : static {
        if (\is_array($prefix) || $prefix instanceof \Traversable) {
            // don't use is_iterable(), it's slow
            foreach ($prefix as $s) {
                $t = $this->trimPrefix($s);
                if ($t->string !== $this->string) {
                    return $t;
                }
            }
            return clone $this;
        }
        $str = clone $this;
        if ($prefix instanceof self) {
            $prefix = $prefix->string;
        }
        else {
            $prefix = (string) $prefix;
        }
        if ('' !== $prefix && \strlen($this->string) >= \strlen($prefix) && 0 === substr_compare($this->string, $prefix, 0, \strlen($prefix), $this->ignoreCase)) {
            $str->string = substr($this->string, \strlen($prefix));
        }
        return $str;
    }
    public abstract function trimStart(string $chars = " \t\n\r\x00\v\f ") : static;
    
    /**
     * @param string|string[] $suffix
     */
    public function trimSuffix($suffix) : static {
        if (\is_array($suffix) || $suffix instanceof \Traversable) {
            // don't use is_iterable(), it's slow
            foreach ($suffix as $s) {
                $t = $this->trimSuffix($s);
                if ($t->string !== $this->string) {
                    return $t;
                }
            }
            return clone $this;
        }
        $str = clone $this;
        if ($suffix instanceof self) {
            $suffix = $suffix->string;
        }
        else {
            $suffix = (string) $suffix;
        }
        if ('' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase)) {
            $str->string = substr($this->string, 0, -\strlen($suffix));
        }
        return $str;
    }
    public function truncate(int $length, string $ellipsis = '', bool|TruncateMode $cut = TruncateMode::Char) : static {
        $stringLength = $this->length();
        if ($stringLength <= $length) {
            return clone $this;
        }
        $ellipsisLength = '' !== $ellipsis ? (new static($ellipsis))->length() : 0;
        if ($length < $ellipsisLength) {
            $ellipsisLength = 0;
        }
        $desiredLength = $length;
        if (TruncateMode::WordAfter === $cut || !$cut) {
            if (null === ($length = $this->indexOf([
                ' ',
                "\r",
                "\n",
                "\t",
            ], ($length ?: 1) - 1))) {
                return clone $this;
            }
            $length += $ellipsisLength;
        }
        elseif (TruncateMode::WordBefore === $cut && null !== $this->indexOf([
            ' ',
            "\r",
            "\n",
            "\t",
        ], ($length ?: 1) - 1)) {
            $length += $ellipsisLength;
        }
        $str = $this->slice(0, $length - $ellipsisLength);
        if (TruncateMode::WordBefore === $cut) {
            if (0 === $ellipsisLength && $desiredLength === $this->indexOf([
                ' ',
                "\r",
                "\n",
                "\t",
            ], $length)) {
                return $str;
            }
            $str = $str->beforeLast([
                ' ',
                "\r",
                "\n",
                "\t",
            ]);
        }
        return $ellipsisLength ? $str->trimEnd()
            ->append($ellipsis) : $str;
    }
    public abstract function upper() : static;
    
    /**
     * Returns the printable length on a terminal.
     */
    public abstract function width(bool $ignoreAnsiDecoration = true) : int;
    public function wordwrap(int $width = 75, string $break = "\n", bool $cut = false) : static {
        $lines = '' !== $break ? $this->split($break) : [
            clone $this,
        ];
        $chars = [];
        $mask = '';
        if (1 === \count($lines) && '' === $lines[0]->string) {
            return $lines[0];
        }
        foreach ($lines as $i => $line) {
            if ($i) {
                $chars[] = $break;
                $mask .= '#';
            }
            foreach ($line->chunk() as $char) {
                $chars[] = $char->string;
                $mask .= ' ' === $char->string ? ' ' : '?';
            }
        }
        $string = '';
        $j = 0;
        $b = $i = -1;
        $mask = wordwrap($mask, $width, '#', $cut);
        while (false !== ($b = strpos($mask, '#', $b + 1))) {
            for (++$i; $i < $b; ++$i) {
                $string .= $chars[$j];
                unset($chars[$j++]);
            }
            if ($break === $chars[$j] || ' ' === $chars[$j]) {
                unset($chars[$j++]);
            }
            $string .= $break;
        }
        $str = clone $this;
        $str->string = $string . implode('', $chars);
        return $str;
    }
    public function __sleep() : array {
        return [
            'string',
        ];
    }
    public function __clone() {
        $this->ignoreCase = false;
    }
    public function __toString() : string {
        return $this->string;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AbstractString::$ignoreCase protected property
AbstractString::$string protected property
AbstractString::after public function
AbstractString::afterLast public function
AbstractString::append abstract public function 3
AbstractString::before public function
AbstractString::beforeLast public function
AbstractString::bytesAt public function 1
AbstractString::camel abstract public function 2
AbstractString::chunk abstract public function 3
AbstractString::collapseWhitespace public function
AbstractString::containsAny public function
AbstractString::endsWith public function 3
AbstractString::ensureEnd public function
AbstractString::ensureStart public function
AbstractString::equalsTo public function 3
AbstractString::folded abstract public function 2
AbstractString::ignoreCase public function
AbstractString::indexOf public function 3
AbstractString::indexOfLast public function 3
AbstractString::isEmpty public function
AbstractString::join abstract public function 2
AbstractString::jsonSerialize public function
AbstractString::kebab public function
AbstractString::length abstract public function 3
AbstractString::lower abstract public function 2
AbstractString::match abstract public function Matches the string using a regular expression. 2
AbstractString::padBoth abstract public function 2
AbstractString::padEnd abstract public function 2
AbstractString::padStart abstract public function 2
AbstractString::PREG_OFFSET_CAPTURE public constant
AbstractString::PREG_PATTERN_ORDER public constant
AbstractString::PREG_SET_ORDER public constant
AbstractString::PREG_SPLIT public constant
AbstractString::PREG_SPLIT_DELIM_CAPTURE public constant
AbstractString::PREG_SPLIT_NO_EMPTY public constant
AbstractString::PREG_SPLIT_OFFSET_CAPTURE public constant
AbstractString::PREG_UNMATCHED_AS_NULL public constant
AbstractString::prepend abstract public function 3
AbstractString::repeat public function
AbstractString::replace abstract public function 3
AbstractString::replaceMatches abstract public function 2
AbstractString::reverse abstract public function 2
AbstractString::slice abstract public function 3
AbstractString::snake abstract public function 2
AbstractString::splice abstract public function 3
AbstractString::split public function 3
AbstractString::startsWith public function 3
AbstractString::title abstract public function 2
AbstractString::toByteString public function
AbstractString::toCodePointString public function 1
AbstractString::toString public function
AbstractString::toUnicodeString public function 1
AbstractString::trim abstract public function 2
AbstractString::trimEnd abstract public function 2
AbstractString::trimPrefix public function 1
AbstractString::trimStart abstract public function 2
AbstractString::trimSuffix public function 1
AbstractString::truncate public function
AbstractString::unwrap public static function Unwraps instances of AbstractString back to strings.
AbstractString::upper abstract public function 2
AbstractString::width abstract public function Returns the printable length on a terminal. 2
AbstractString::wordwrap public function
AbstractString::wrap public static function Wraps (and normalizes) strings in instances of AbstractString.
AbstractString::__clone public function 1
AbstractString::__construct abstract public function 3
AbstractString::__sleep public function
AbstractString::__toString public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal