Skip to content

Commit ba65a96

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: (40 commits) [PropertyInfo] treat mixed[] the same as array when getting types from docblocks treat `mixed[]` the same as `array` when getting types from docblocks install ext-zstd on PHP 8.5 as well fix merge [Console] Fix profile invokable command sync ControllerHelper docblock with latest AbstractController changes fix: Typehint for `createForm` in abstractController [Notifier][Mercure] Add support for Mercure 0.7 register attribute loader arguments in a forward-compatible way ensure compatibility with RelayCluster 0.20.0 mark test using a Redis connection as an integration test ensure compatibility with Relay extension 0.20.0 [FrameworkBundle] Allow backed enum to be used in initial_marking workflow configuration [DependencyInjection] Fix `query_string` env processor for URLs without query string [HttpFoundation] Fix Expires response header for EventStream Bump Symfony version to 7.4.1 Update VERSION for 7.4.0 Update CHANGELOG for 7.4.0 - [DependencyInjection] Fix state corruption in PhpFileLoader during recursive imports ...
2 parents f929ecc + d50e862 commit ba65a96

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Tests/UnicodeStringTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,58 @@
1111

1212
namespace Symfony\Component\String\Tests;
1313

14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use Symfony\Component\String\AbstractString;
1516
use Symfony\Component\String\UnicodeString;
1617

1718
class UnicodeStringTest extends AbstractUnicodeTestCase
1819
{
20+
#[DataProvider('provideTrimNormalization')]
21+
public function testTrimPrefixNormalization(string $expected, string $string, $prefix)
22+
{
23+
$str = new UnicodeString($string);
24+
$this->assertSame($expected, $str->trimPrefix($prefix)->toString());
25+
}
26+
27+
#[DataProvider('provideTrimNormalization')]
28+
public function testTrimSuffixNormalization(string $expected, string $string, $suffix)
29+
{
30+
$suffixStr = match (true) {
31+
$suffix instanceof AbstractString => $suffix->toString(),
32+
\is_array($suffix) => implode('', $suffix),
33+
$suffix instanceof \Traversable => implode('', iterator_to_array($suffix)),
34+
default => (string) $suffix,
35+
};
36+
37+
$str = new UnicodeString($expected.$suffixStr);
38+
$this->assertSame($expected, $str->trimSuffix($suffix)->toString());
39+
}
40+
41+
public static function provideTrimNormalization(): iterable
42+
{
43+
// "é" in NFC (\xC3\xA9) vs NFD (\x65\xCC\x81)
44+
$nfc = "\xC3\xA9";
45+
$nfd = "\x65\xCC\x81";
46+
47+
yield 'nfc_string_nfd_prefix' => ['abc', $nfc.'abc', $nfd];
48+
yield 'nfd_string_nfc_prefix' => ['abc', $nfd.'abc', $nfc];
49+
50+
yield 'abstract_string' => ['abc', $nfc.'abc', new UnicodeString($nfd)];
51+
52+
yield 'array' => ['abc', $nfc.'abc', [$nfd]];
53+
54+
yield 'stringable' => ['abc', $nfc.'abc', new class($nfd) implements \Stringable {
55+
public function __construct(private string $s)
56+
{
57+
}
58+
59+
public function __toString(): string
60+
{
61+
return $this->s;
62+
}
63+
}];
64+
}
65+
1966
protected static function createFromString(string $string): AbstractString
2067
{
2168
return new UnicodeString($string);

UnicodeString.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,44 @@ public function startsWith(string|iterable|AbstractString $prefix): bool
366366
return $prefix === $grapheme;
367367
}
368368

369+
public function trimPrefix($prefix): static
370+
{
371+
if (\is_array($prefix) || $prefix instanceof \Traversable) {
372+
return parent::trimPrefix($prefix);
373+
}
374+
375+
if ($prefix instanceof AbstractString) {
376+
$prefix = $prefix->string;
377+
} else {
378+
$prefix = (string) $prefix;
379+
}
380+
381+
if (!normalizer_is_normalized($prefix, \Normalizer::NFC)) {
382+
$prefix = normalizer_normalize($prefix, \Normalizer::NFC);
383+
}
384+
385+
return parent::trimPrefix($prefix);
386+
}
387+
388+
public function trimSuffix($suffix): static
389+
{
390+
if (\is_array($suffix) || $suffix instanceof \Traversable) {
391+
return parent::trimSuffix($suffix);
392+
}
393+
394+
if ($suffix instanceof AbstractString) {
395+
$suffix = $suffix->string;
396+
} else {
397+
$suffix = (string) $suffix;
398+
}
399+
400+
if (!normalizer_is_normalized($suffix, \Normalizer::NFC)) {
401+
$suffix = normalizer_normalize($suffix, \Normalizer::NFC);
402+
}
403+
404+
return parent::trimSuffix($suffix);
405+
}
406+
369407
public function __unserialize(array $data): void
370408
{
371409
$this->string = $data['string'] ?? $data["\0*\0string"];

0 commit comments

Comments
 (0)