Skip to content

Commit 9d7be92

Browse files
committed
Added Collection::implode($glue, $callback)
1 parent 986674d commit 9d7be92

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Collection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ public function isEmpty()
338338
return empty($this->items);
339339
}
340340

341+
/**
342+
* Implode the collection values.
343+
*
344+
* @see implode()
345+
* @param string $glue
346+
* @param callable|null $callback
347+
* @return string
348+
*/
349+
public function implode($glue = '', callable $callback = null)
350+
{
351+
return implode($glue, $callback ? array_map($callback, $this->items) : $this->items);
352+
}
353+
341354

342355
/**
343356
* @InheritDoc

tests/Unit/CollectionTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public function testHas()
328328
$this->assertFalse($collection->has('four'));
329329
}
330330

331-
public function testEmpty()
331+
public function testIsEmpty()
332332
{
333333
$items1 = [];
334334
$items2 = [1,2,3,4];
@@ -339,6 +339,16 @@ public function testEmpty()
339339
$this->assertFalse($collection2->isEmpty());
340340
}
341341

342+
public function testImplode()
343+
{
344+
$items = [1.5,2.5,3.5,4.5];
345+
$collection = new Collection($items);
346+
347+
$this->assertEquals('1.5,2.5,3.5,4.5', $collection->implode(','));
348+
$this->assertEquals('1,2,3,4', $collection->implode(',', 'floor'));
349+
$this->assertEquals('2,3,4,5', $collection->implode(',', 'ceil'));
350+
}
351+
342352
public function testToArray()
343353
{
344354
$items = [1,2,3,4];

0 commit comments

Comments
 (0)