Skip to content

Commit d9f3c5f

Browse files
authored
Merge pull request #10 from chadicus/master
Add Arrays::implode
2 parents 3c630e1 + ba4f675 commit d9f3c5f

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

.github/workflows/php.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ on:
88

99
jobs:
1010
build:
11-
runs-on: ubuntu-18.04
11+
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
php-versions: ['7.3', '7.4', '8.0', '8.1']
14+
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v2

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ $value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
7575
assert($value === [1, 2, 3, 4, 5]);
7676
```
7777

78+
#### Arrays::implode
79+
80+
This filter is a wrapper to the PHP `implode` function. It joins an array of strings with the optional glue string.
81+
```php
82+
$value = \TraderInteractive\Filter\Arrays::implode(['lastname', 'email', 'phone'], ',');
83+
assert($value === 'lastname,email,phone');
84+
```
85+
7886
#### Arrays::pad
7987

8088
This filter pads an array to the specified length with a value. Padding optionally to the front or end of the array.

src/Arrays.php

+13
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ public static function pad(array $input, int $size, $padValue = null, int $padTy
211211
return $input;
212212
}
213213

214+
/**
215+
* Joins array elements with a string.
216+
*
217+
* @param array $input The array to be filtered.
218+
* @param string $glue The string to which each array element should be joined.
219+
*
220+
* @return string
221+
*/
222+
public static function implode(array $input, string $glue = '') : string
223+
{
224+
return implode($glue, $input);
225+
}
226+
214227
/**
215228
* Removes duplicate values from an array.
216229
*

tests/ArraysTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,25 @@ public function uniqueStrict()
267267
$this->expectExceptionMessage($expectedException->getMessage());
268268
Arrays::unique($input, Arrays::ARRAY_UNIQUE_SORT_STRING, true);
269269
}
270+
271+
/**
272+
* @test
273+
* @covers ::implode
274+
*/
275+
public function implodeWithGlue()
276+
{
277+
$glue = '_';
278+
$input = [0, 1, 2];
279+
$this->assertSame('0_1_2', Arrays::implode($input, $glue));
280+
}
281+
282+
/**
283+
* @test
284+
* @covers ::implode
285+
*/
286+
public function implodeWithDefaultGlue()
287+
{
288+
$input = ['a', 'b', 'c'];
289+
$this->assertSame('abc', Arrays::implode($input));
290+
}
270291
}

0 commit comments

Comments
 (0)