Skip to content

Commit

Permalink
Ensure implode works with stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Nov 26, 2024
1 parent 5edb71f commit f6ecef1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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
33 changes: 33 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,38 @@ 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( ',' ) );
}

/**
* @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

0 comments on commit f6ecef1

Please sign in to comment.