Skip to content

Commit 2118912

Browse files
Merge branch '5.4' into 6.0
* 5.4: [HttpClient] fix RetryableHttpClient when a response is canceled Deprecate passing null as $requestIp to IpUtils::checkIp(), checkIp4() and checkIp6() [Uid] fix 4 missing bits of entropy in UUIDv4 Add a warning in WDT if using symfony/symfony [Notifier][Twilio] Ensure from/sender is valid via regex Lower log level in case of retry GuardEvent::getTransition() cannot return null [String] Add `trimSuffix()` and `trimPrefix()` methods [DependencyInjection] autowire union and intersection types [Runtime] Drop class validation of composer "extra.runtime.class"
2 parents 966d73c + 37db45d commit 2118912

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

AbstractString.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,72 @@ abstract public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}")
556556

557557
abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): static;
558558

559+
/**
560+
* @param string|string[] $prefix
561+
*/
562+
public function trimPrefix($prefix): static
563+
{
564+
if (\is_array($prefix) || $prefix instanceof \Traversable) {
565+
foreach ($prefix as $s) {
566+
$t = $this->trimPrefix($s);
567+
568+
if ($t->string !== $this->string) {
569+
return $t;
570+
}
571+
}
572+
573+
return clone $this;
574+
}
575+
576+
$str = clone $this;
577+
578+
if ($prefix instanceof self) {
579+
$prefix = $prefix->string;
580+
} else {
581+
$prefix = (string) $prefix;
582+
}
583+
584+
if ('' !== $prefix && \strlen($this->string) >= \strlen($prefix) && 0 === substr_compare($this->string, $prefix, 0, \strlen($prefix), $this->ignoreCase)) {
585+
$str->string = substr($this->string, \strlen($prefix));
586+
}
587+
588+
return $str;
589+
}
590+
559591
abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): static;
560592

593+
/**
594+
* @param string|string[] $suffix
595+
*/
596+
public function trimSuffix($suffix): static
597+
{
598+
if (\is_array($suffix) || $suffix instanceof \Traversable) {
599+
foreach ($suffix as $s) {
600+
$t = $this->trimSuffix($s);
601+
602+
if ($t->string !== $this->string) {
603+
return $t;
604+
}
605+
}
606+
607+
return clone $this;
608+
}
609+
610+
$str = clone $this;
611+
612+
if ($suffix instanceof self) {
613+
$suffix = $suffix->string;
614+
} else {
615+
$suffix = (string) $suffix;
616+
}
617+
618+
if ('' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase)) {
619+
$str->string = substr($this->string, 0, -\strlen($suffix));
620+
}
621+
622+
return $str;
623+
}
624+
561625
public function truncate(int $length, string $ellipsis = '', bool $cut = true): static
562626
{
563627
$stringLength = $this->length();

AbstractUnicodeString.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,26 @@ public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): stat
399399
return $str;
400400
}
401401

402+
public function trimPrefix($prefix): static
403+
{
404+
if (!$this->ignoreCase) {
405+
return parent::trimPrefix($prefix);
406+
}
407+
408+
$str = clone $this;
409+
410+
if ($prefix instanceof \Traversable) {
411+
$prefix = iterator_to_array($prefix, false);
412+
} elseif ($prefix instanceof parent) {
413+
$prefix = $prefix->string;
414+
}
415+
416+
$prefix = implode('|', array_map('preg_quote', (array) $prefix));
417+
$str->string = preg_replace("{^(?:$prefix)}iuD", '', $this->string);
418+
419+
return $str;
420+
}
421+
402422
public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): static
403423
{
404424
if (" \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}" !== $chars && !preg_match('//u', $chars)) {
@@ -412,6 +432,26 @@ public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): st
412432
return $str;
413433
}
414434

435+
public function trimSuffix($suffix): static
436+
{
437+
if (!$this->ignoreCase) {
438+
return parent::trimSuffix($suffix);
439+
}
440+
441+
$str = clone $this;
442+
443+
if ($suffix instanceof \Traversable) {
444+
$suffix = iterator_to_array($suffix, false);
445+
} elseif ($suffix instanceof parent) {
446+
$suffix = $suffix->string;
447+
}
448+
449+
$suffix = implode('|', array_map('preg_quote', (array) $suffix));
450+
$str->string = preg_replace("{(?:$suffix)$}iuD", '', $this->string);
451+
452+
return $str;
453+
}
454+
415455
public function upper(): static
416456
{
417457
$str = clone $this;

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `trimSuffix()` and `trimPrefix()` methods
8+
49
5.3
510
---
611

Tests/AbstractAsciiTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,15 @@ public static function provideTrim()
733733
];
734734
}
735735

736+
public function testTrimPrefix()
737+
{
738+
$str = static::createFromString('abc.def');
739+
740+
$this->assertEquals(static::createFromString('def'), $str->trimPrefix('abc.'));
741+
$this->assertEquals(static::createFromString('def'), $str->trimPrefix(['abc.', 'def']));
742+
$this->assertEquals(static::createFromString('def'), $str->ignoreCase()->trimPrefix('ABC.'));
743+
}
744+
736745
/**
737746
* @dataProvider provideTrimStart
738747
*/
@@ -744,6 +753,15 @@ public function testTrimStart(string $expected, string $origin, ?string $chars)
744753
$this->assertEquals(static::createFromString($expected), $result);
745754
}
746755

756+
public function testTrimSuffix()
757+
{
758+
$str = static::createFromString('abc.def');
759+
760+
$this->assertEquals(static::createFromString('abc'), $str->trimSuffix('.def'));
761+
$this->assertEquals(static::createFromString('abc'), $str->trimSuffix(['.def', 'abc']));
762+
$this->assertEquals(static::createFromString('abc'), $str->ignoreCase()->trimSuffix('.DEF'));
763+
}
764+
747765
public static function provideTrimStart()
748766
{
749767
return [

0 commit comments

Comments
 (0)