From c00317d8103809b21104d651ad3a161625280f6f Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 14 Sep 2020 12:41:09 -0400 Subject: [PATCH 1/4] Refactor tests for Strings::explode --- tests/Filter/StringsTest.php | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index 871298c..ed2e416 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -224,25 +224,39 @@ public function translateValueNotFoundInMap() } /** - * Verifies basic explode functionality. + * @param string $value The value to be filtered. + * @param array $expectedResult The expected filter result. + * @param string $delimiter The delimiter to use. * * @test * @covers ::explode + * @dataProvider provideExplodeData */ - public function explode() - { - $this->assertSame(['a', 'bcd', 'e'], Strings::explode('a,bcd,e')); + public function explode( + string $value, + array $expectedResult, + string $delimiter = ',' + ) { + $actualResult = Strings::explode($value, $delimiter); + $this->assertSame($expectedResult, $actualResult); } /** - * Verifies explode with a custom delimiter. - * - * @test - * @covers ::explode + * @return array */ - public function explodeCustomDelimiter() + public function provideExplodeData() : array { - $this->assertSame(['a', 'b', 'c', 'd,e'], Strings::explode('a b c d,e', ' ')); + return [ + [ + 'value' => 'a,bcd,e', + 'result' => ['a', 'bcd', 'e'], + ], + [ + 'value' => 'a b c d,e', + 'result' => ['a', 'b', 'c', 'd,e'], + 'delimiter' => ' ', + ], + ]; } /** From f928dc1690245a49793ecb42d0506d1ae5b22d78 Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 14 Sep 2020 12:44:48 -0400 Subject: [PATCH 2/4] Allow padding of result in Strings::explode --- README.md | 6 ++++- src/Filter/Strings.php | 46 ++++++++++++++++++++++++++++++++---- tests/Filter/StringsTest.php | 37 +++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f1bb5d7..dd7410b 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,11 @@ with the value first in order to work with the `Filterer`. It also defaults to $value = \TraderInteractive\Filter\Strings::explode('abc,def,ghi'); assert($value === ['abc', 'def', 'ghi']); ``` - +This filter can also be given padding settings to ensure the result has a specific number of elements. For example: +```php +$value = Strings::explode('a-b-c', '-', 5, null, Strings::EXPLODE_PAD_FRONT); +assert($value === [null, null, 'a', 'b', 'c']); +``` #### Strings::compress This filter trims and remove superfluous whitespace from a given string. ```php diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index 3f3c345..3a37cbf 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -2,6 +2,7 @@ namespace TraderInteractive\Filter; +use InvalidArgumentException; use TraderInteractive\Exceptions\FilterException; use TypeError; @@ -10,6 +11,16 @@ */ final class Strings { + /** + * @var string + */ + const EXPLODE_PAD_FRONT = 'front'; + + /** + * @var string + */ + const EXPLODE_PAD_END = 'end'; + /** * Filter a string. * @@ -52,14 +63,23 @@ public static function filter( * * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz']. * - * @param string $value The string to explode. - * @param string $delimiter The non-empty delimiter to explode on. + * @param string $value The string to explode. + * @param string $delimiter The non-empty delimiter to explode on. + * @param int|null $padLength The number of elements to be returned in the result. + * @param mixed $padValue The value to use when padding the resulting array. + * @param string $padType Argument to specify if the resulting array should be padded on the left or right. + * * @return array The exploded values. * * @throws \InvalidArgumentException if the delimiter does not pass validation. */ - public static function explode($value, string $delimiter = ',') - { + public static function explode( + $value, + string $delimiter = ',', + int $padLength = null, + $padValue = null, + string $padType = self::EXPLODE_PAD_END + ) : array { self::validateIfObjectIsAString($value); if (empty($delimiter)) { @@ -68,7 +88,23 @@ public static function explode($value, string $delimiter = ',') ); } - return explode($delimiter, $value); + $values = explode($delimiter, $value); + $padLength = $padLength ?? count($values); + while (count($values) < $padLength) { + if ($padType === self::EXPLODE_PAD_END) { + array_push($values, $padValue); + continue; + } + + if ($padType === self::EXPLODE_PAD_FRONT) { + array_unshift($values, $padValue); + continue; + } + + throw new InvalidArgumentException('Invalid $padType value provided'); + } + + return $values; } /** diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index ed2e416..276c4e9 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -227,6 +227,9 @@ public function translateValueNotFoundInMap() * @param string $value The value to be filtered. * @param array $expectedResult The expected filter result. * @param string $delimiter The delimiter to use. + * @param int|null $padLength The array length of the result. + * @param mixed $padValue The value to use when padding. + * @param string $padType The direction to pad the resulting array. * * @test * @covers ::explode @@ -235,9 +238,12 @@ public function translateValueNotFoundInMap() public function explode( string $value, array $expectedResult, - string $delimiter = ',' + string $delimiter = ',', + int $padLength = null, + $padValue = null, + string $padType = Strings::EXPLODE_PAD_END ) { - $actualResult = Strings::explode($value, $delimiter); + $actualResult = Strings::explode($value, $delimiter, $padLength, $padValue, $padType); $this->assertSame($expectedResult, $actualResult); } @@ -256,9 +262,36 @@ public function provideExplodeData() : array 'result' => ['a', 'b', 'c', 'd,e'], 'delimiter' => ' ', ], + [ + 'value' => 'a-b-c', + 'result' => ['a', 'b', 'c', null, null], + 'delimiter' => '-', + 'padLength' => 5, + 'padValue' => null, + 'padType' => Strings::EXPLODE_PAD_END, + ], + [ + 'value' => 'a-b-c', + 'result' => [null, null, 'a', 'b', 'c'], + 'delimiter' => '-', + 'padLength' => 5, + 'padValue' => null, + 'padType' => Strings::EXPLODE_PAD_FRONT, + ], ]; } + /** + * @test + * @covers ::explode + */ + public function explodeWithInvalidPadType() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid $padType value provided'); + Strings::explode('a,b,c', ',', 4, null, 'invalid'); + } + /** * @test * @covers ::explode From a5c271922d9ff3b39b47b4fec7931e51fdfc3187 Mon Sep 17 00:00:00 2001 From: chadicus Date: Thu, 15 Oct 2026 17:03:00 -0400 Subject: [PATCH 3/4] check padType before loop --- src/Filter/Strings.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index 3a37cbf..5c167ff 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -88,6 +88,10 @@ public static function explode( ); } + if ($padType !== self::EXPLODE_PAD_FRONT && $padType !== self::EXPLODE_PAD_END) { + throw new InvalidArgumentException('Invalid $padType value provided'); + } + $values = explode($delimiter, $value); $padLength = $padLength ?? count($values); while (count($values) < $padLength) { @@ -96,12 +100,7 @@ public static function explode( continue; } - if ($padType === self::EXPLODE_PAD_FRONT) { - array_unshift($values, $padValue); - continue; - } - - throw new InvalidArgumentException('Invalid $padType value provided'); + array_unshift($values, $padValue); } return $values; From 55fd8e679749a6c807479e29738df8baa6f1bfdf Mon Sep 17 00:00:00 2001 From: chadicus Date: Thu, 15 Oct 2026 17:16:02 -0400 Subject: [PATCH 4/4] Use single calls to array_unshift and array_push --- src/Filter/Strings.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index 5c167ff..f2f8d53 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -93,16 +93,19 @@ public static function explode( } $values = explode($delimiter, $value); - $padLength = $padLength ?? count($values); - while (count($values) < $padLength) { - if ($padType === self::EXPLODE_PAD_END) { - array_push($values, $padValue); - continue; - } + $valueCount = count($values); + $padLength = $padLength ?? $valueCount; + if ($padLength <= $valueCount) { + return $values; + } - array_unshift($values, $padValue); + $padding = array_fill(0, $padLength - $valueCount, $padValue); + if ($padType === self::EXPLODE_PAD_END) { + array_push($values, ...$padding); + return $values; } + array_unshift($values, ...$padding); return $values; }