Skip to content

Commit

Permalink
Merge pull request #10 from chadicus/master
Browse files Browse the repository at this point in the history
Add Arrays::implode
  • Loading branch information
chadicus authored Jan 3, 2024
2 parents 3c630e1 + ba4f675 commit d9f3c5f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ on:

jobs:
build:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0', '8.1']
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ $value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
assert($value === [1, 2, 3, 4, 5]);
```

#### Arrays::implode

This filter is a wrapper to the PHP `implode` function. It joins an array of strings with the optional glue string.
```php
$value = \TraderInteractive\Filter\Arrays::implode(['lastname', 'email', 'phone'], ',');
assert($value === 'lastname,email,phone');
```

#### Arrays::pad

This filter pads an array to the specified length with a value. Padding optionally to the front or end of the array.
Expand Down
13 changes: 13 additions & 0 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public static function pad(array $input, int $size, $padValue = null, int $padTy
return $input;
}

/**
* Joins array elements with a string.
*
* @param array $input The array to be filtered.
* @param string $glue The string to which each array element should be joined.
*
* @return string
*/
public static function implode(array $input, string $glue = '') : string
{
return implode($glue, $input);
}

/**
* Removes duplicate values from an array.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,25 @@ public function uniqueStrict()
$this->expectExceptionMessage($expectedException->getMessage());
Arrays::unique($input, Arrays::ARRAY_UNIQUE_SORT_STRING, true);
}

/**
* @test
* @covers ::implode
*/
public function implodeWithGlue()
{
$glue = '_';
$input = [0, 1, 2];
$this->assertSame('0_1_2', Arrays::implode($input, $glue));
}

/**
* @test
* @covers ::implode
*/
public function implodeWithDefaultGlue()
{
$input = ['a', 'b', 'c'];
$this->assertSame('abc', Arrays::implode($input));
}
}

0 comments on commit d9f3c5f

Please sign in to comment.