diff --git a/CHANGELOG.md b/CHANGELOG.md index c6886c70..190ff054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/mantle/support/class-collection.php b/src/mantle/support/class-collection.php index 149231cf..88a11732 100644 --- a/src/mantle/support/class-collection.php +++ b/src/mantle/support/class-collection.php @@ -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() ); } diff --git a/src/mantle/support/class-stringable.php b/src/mantle/support/class-stringable.php index 52f55cf8..e5b995c0 100644 --- a/src/mantle/support/class-stringable.php +++ b/src/mantle/support/class-stringable.php @@ -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; @@ -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; /** diff --git a/tests/Support/CollectionTest.php b/tests/Support/CollectionTest.php index 1873398c..48c95315 100644 --- a/tests/Support/CollectionTest.php +++ b/tests/Support/CollectionTest.php @@ -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; @@ -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 */