Skip to content
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

[8.x] Add chunk() to Fluent Strings #35038

Merged
merged 1 commit into from
Nov 2, 2020
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
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ public function camel()
return new static(Str::camel($this->value));
}

/**
* Split the string into a collection of chunks of a specified length.
*
* @param int $length
* @return \Illuminate\Support\Collection
*/
public function chunk($length)
{
return collect(mb_str_split($this->value, $length));
}

/**
* Determine if a given string contains a given substring.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Illuminate\Support\Collection;
use Illuminate\Support\Stringable;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -528,4 +529,12 @@ public function testPadRight()
$this->assertSame('Alien-----', (string) $this->stringable('Alien')->padRight(10, '-'));
$this->assertSame('Alien ', (string) $this->stringable('Alien')->padRight(10));
}

public function testChunk()
{
$chunks = $this->stringable('foobarbaz')->chunk(3);

$this->assertInstanceOf(Collection::class, $chunks);
$this->assertSame(['foo', 'bar', 'baz'], $chunks->all());
}
}