Skip to content

[String] add $lastGlue argument to join() methods #33914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function isEmpty(): bool
/**
* @return static
*/
abstract public function join(array $strings): self;
abstract public function join(array $strings, string $lastGlue = null): self;

public function jsonSerialize(): string
{
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ public function folded(bool $compat = true): parent
return $str;
}

public function join(array $strings): parent
public function join(array $strings, string $lastGlue = null): parent
{
$str = clone $this;
$str->string = implode($this->string, $strings);

$tail = null !== $lastGlue && 1 < \count($strings) ? $lastGlue.array_pop($strings) : '';
$str->string = implode($this->string, $strings).$tail;

if (!preg_match('//u', $str->string)) {
throw new InvalidArgumentException('Invalid UTF-8 string.');
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ public function isUtf8(): bool
return '' === $this->string || preg_match('//u', $this->string);
}

public function join(array $strings): parent
public function join(array $strings, string $lastGlue = null): parent
{
$str = clone $this;
$str->string = implode($str->string, $strings);

$tail = null !== $lastGlue && 1 < \count($strings) ? $lastGlue.array_pop($strings) : '';
$str->string = implode($this->string, $strings).$tail;

return $str;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,11 @@ public function testJoin(string $expected, string $origin, array $join)
$this->assertEquals(static::createFromString($expected), $instance);
}

public function testJoinWithLastGlue()
{
$this->assertSame('foo, bar and baz', (string) static::createFromString(', ')->join(['foo', 'bar', 'baz'], ' and '));
}

public static function provideJoin()
{
return [
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/String/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public function indexOfLast($needle, int $offset = 0): ?int
return false === $i ? null : $i;
}

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

return $str;
Expand Down