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

Ensure collections can implode Stringable properly #605

Merged
merged 3 commits into from
Dec 2, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure that `with_terms()` can support an array of term slugs when passed with a
taxonomy index.
- Ensure that framework configuration respects the application configuration.
- Ensure that collections can properly implode `Stringable` objects.

## v1.2.0 - 2024-09-23

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/support/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public function implode( $value, $glue = null ) {

$first = $this->first();

if ( is_array( $first ) || is_object( $first ) ) {
if ( is_array( $first ) || ( is_object( $first ) && ! $first instanceof \Stringable ) ) {
return implode( $glue ?? '', $this->pluck( $value )->all() );
}

Expand Down
3 changes: 2 additions & 1 deletion src/mantle/support/class-stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Mantle\Support\Traits\Conditionable;
use Mantle\Support\Traits\Macroable;
use JsonSerializable;
use Mantle\Support\Traits\Makeable;
use Mantle\Support\Traits\Tappable;
use Symfony\Component\VarDumper\VarDumper;

Expand All @@ -24,9 +25,9 @@
* Allows for the chaining of string methods.
*/
class Stringable implements ArrayAccess, JsonSerializable, \Stringable {

use Conditionable;
use Macroable;
use Makeable;
use Tappable;

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Support/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Mantle\Database\Model;
use InvalidArgumentException;
use JsonSerializable;
use Mantle\Support\Stringable;
use Mantle\Testing\Framework_Test_Case;
use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -1753,6 +1754,43 @@ public function testImplode($collection)
$this->assertSame('taylor,dayle', $data->implode(','));
}

/**
* @dataProvider collectionClassProvider
*/
#[DataProvider( 'collectionClassProvider' )]
public function testImplodeStringable($collection)
{
$data = new $collection( [
Stringable::make( 'example' ),
Stringable::make( 'string' ),
Stringable::make( 'here' ),
] );

$this->assertSame( 'example string here', $data->implode( ' ' ) );
$this->assertSame( 'example,string,here', $data->implode( ',' ) );

$this->assertEquals(
'another-example-here',
Stringable::make( 'another example here' )->explode( ' ' )->implode( '-' ),
);
}

/**
* @dataProvider collectionClassProvider
*/
#[DataProvider( 'collectionClassProvider' )]
public function testImplodeStr($collection)
{
$data = new $collection( [
Stringable::make( 'example' ),
Stringable::make( 'string' ),
Stringable::make( 'here' ),
] );

$this->assertInstanceof( \Mantle\Support\Stringable::class, $data->implode_str( ' ' ) );
$this->assertSame( 'example string here', $data->implode_str( ' ' )->value() );
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down