This commit is contained in:
2025-05-12 14:25:25 +02:00
parent ab2db755ef
commit 9e378ca2b7
2719 changed files with 46505 additions and 60181 deletions

View File

@@ -11,6 +11,7 @@
namespace Symfony\Component\String;
use Random\Randomizer;
use Symfony\Component\String\Exception\ExceptionInterface;
use Symfony\Component\String\Exception\InvalidArgumentException;
use Symfony\Component\String\Exception\RuntimeException;
@@ -42,10 +43,10 @@ class ByteString extends AbstractString
* Copyright (c) 2004-2020, Facebook, Inc. (https://www.facebook.com/)
*/
public static function fromRandom(int $length = 16, string $alphabet = null): self
public static function fromRandom(int $length = 16, ?string $alphabet = null): self
{
if ($length <= 0) {
throw new InvalidArgumentException(sprintf('A strictly positive length is expected, "%d" given.', $length));
throw new InvalidArgumentException(\sprintf('A strictly positive length is expected, "%d" given.', $length));
}
$alphabet ??= self::ALPHABET_ALPHANUMERIC;
@@ -55,6 +56,10 @@ class ByteString extends AbstractString
throw new InvalidArgumentException('The length of the alphabet must in the [2^1, 2^56] range.');
}
if (\PHP_VERSION_ID >= 80300) {
return new static((new Randomizer())->getBytesFromString($alphabet, $length));
}
$ret = '';
while ($length > 0) {
$urandomLength = (int) ceil(2 * $length * $bits / 8.0);
@@ -205,7 +210,7 @@ class ByteString extends AbstractString
return '' === $this->string || preg_match('//u', $this->string);
}
public function join(array $strings, string $lastGlue = null): static
public function join(array $strings, ?string $lastGlue = null): static
{
$str = clone $this;
@@ -332,10 +337,10 @@ class ByteString extends AbstractString
return $str;
}
public function slice(int $start = 0, int $length = null): static
public function slice(int $start = 0, ?int $length = null): static
{
$str = clone $this;
$str->string = (string) substr($this->string, $start, $length ?? \PHP_INT_MAX);
$str->string = substr($this->string, $start, $length ?? \PHP_INT_MAX);
return $str;
}
@@ -348,7 +353,7 @@ class ByteString extends AbstractString
return $str;
}
public function splice(string $replacement, int $start = 0, int $length = null): static
public function splice(string $replacement, int $start = 0, ?int $length = null): static
{
$str = clone $this;
$str->string = substr_replace($this->string, $replacement, $start, $length ?? \PHP_INT_MAX);
@@ -356,7 +361,7 @@ class ByteString extends AbstractString
return $str;
}
public function split(string $delimiter, int $limit = null, int $flags = null): array
public function split(string $delimiter, ?int $limit = null, ?int $flags = null): array
{
if (1 > $limit ??= \PHP_INT_MAX) {
throw new InvalidArgumentException('Split limit must be a positive integer.');
@@ -402,12 +407,12 @@ class ByteString extends AbstractString
return $str;
}
public function toUnicodeString(string $fromEncoding = null): UnicodeString
public function toUnicodeString(?string $fromEncoding = null): UnicodeString
{
return new UnicodeString($this->toCodePointString($fromEncoding)->string);
}
public function toCodePointString(string $fromEncoding = null): CodePointString
public function toCodePointString(?string $fromEncoding = null): CodePointString
{
$u = new CodePointString();
@@ -436,7 +441,7 @@ class ByteString extends AbstractString
}
if (!$validEncoding) {
throw new InvalidArgumentException(sprintf('Invalid "%s" string.', $fromEncoding ?? 'Windows-1252'));
throw new InvalidArgumentException(\sprintf('Invalid "%s" string.', $fromEncoding ?? 'Windows-1252'));
}
$u->string = mb_convert_encoding($this->string, 'UTF-8', $fromEncoding ?? 'Windows-1252');