Skip to content

Commit

Permalink
Ensure collections can implode Stringable properly (#605)
Browse files Browse the repository at this point in the history
* Ensure implode works with stringable

* Provide another example validating the stringable explode and then re-imploding

* CHANGELOG
  • Loading branch information
srtfisher authored Dec 2, 2024
1 parent 5edb71f commit 6131a85
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
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

0 comments on commit 6131a85

Please sign in to comment.