From e42f60d53b3a47318c902020953a602e85ee9d3e Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 3 Jan 2024 11:38:30 -0500 Subject: [PATCH 1/2] Add Arrays::implode --- README.md | 8 ++++++++ src/Arrays.php | 13 +++++++++++++ tests/ArraysTest.php | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/README.md b/README.md index cc70666..7542d33 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Arrays.php b/src/Arrays.php index ee27b33..db6e4bb 100644 --- a/src/Arrays.php +++ b/src/Arrays.php @@ -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. * diff --git a/tests/ArraysTest.php b/tests/ArraysTest.php index 12b3a8d..2fa0d6f 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -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)); + } } From ba4f675a7abac8229de8a22063e92b89cd2fd5a6 Mon Sep 17 00:00:00 2001 From: chadicus Date: Wed, 3 Jan 2024 11:41:14 -0500 Subject: [PATCH 2/2] Add PHP 8.2 and 8.3 to build matrix --- .github/workflows/php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index b85b2e4..b99f645 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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