Skip to content

Commit 1191978

Browse files
[String] add $lastGlue argument to join() methods
1 parent d8597f4 commit 1191978

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

AbstractString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public function isEmpty(): bool
387387
/**
388388
* @return static
389389
*/
390-
abstract public function join(array $strings): self;
390+
abstract public function join(array $strings, string $lastGlue = null): self;
391391

392392
public function jsonSerialize(): string
393393
{

AbstractUnicodeString.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@ public function folded(bool $compat = true): parent
180180
return $str;
181181
}
182182

183-
public function join(array $strings): parent
183+
public function join(array $strings, string $lastGlue = null): parent
184184
{
185185
$str = clone $this;
186-
$str->string = implode($this->string, $strings);
186+
187+
$tail = null !== $lastGlue && 1 < \count($strings) ? $lastGlue.array_pop($strings) : '';
188+
$str->string = implode($this->string, $strings).$tail;
187189

188190
if (!preg_match('//u', $str->string)) {
189191
throw new InvalidArgumentException('Invalid UTF-8 string.');

ByteString.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ public function isUtf8(): bool
168168
return '' === $this->string || preg_match('//u', $this->string);
169169
}
170170

171-
public function join(array $strings): parent
171+
public function join(array $strings, string $lastGlue = null): parent
172172
{
173173
$str = clone $this;
174-
$str->string = implode($str->string, $strings);
174+
175+
$tail = null !== $lastGlue && 1 < \count($strings) ? $lastGlue.array_pop($strings) : '';
176+
$str->string = implode($this->string, $strings).$tail;
175177

176178
return $str;
177179
}

Tests/AbstractAsciiTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,11 @@ public function testJoin(string $expected, string $origin, array $join)
11601160
$this->assertEquals(static::createFromString($expected), $instance);
11611161
}
11621162

1163+
public function testJoinWithLastGlue()
1164+
{
1165+
$this->assertSame('foo, bar and baz', (string) static::createFromString(', ')->join(['foo', 'bar', 'baz'], ' and '));
1166+
}
1167+
11631168
public static function provideJoin()
11641169
{
11651170
return [

UnicodeString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ public function indexOfLast($needle, int $offset = 0): ?int
178178
return false === $i ? null : $i;
179179
}
180180

181-
public function join(array $strings): AbstractString
181+
public function join(array $strings, string $lastGlue = null): AbstractString
182182
{
183-
$str = parent::join($strings);
183+
$str = parent::join($strings, $lastGlue);
184184
normalizer_is_normalized($str->string) ?: $str->string = normalizer_normalize($str->string);
185185

186186
return $str;

0 commit comments

Comments
 (0)