Skip to content

Commit

Permalink
[String] add $lastGlue argument to join() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Oct 8, 2019
1 parent bb392bc commit 714d629
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
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

0 comments on commit 714d629

Please sign in to comment.