From 891411e2315ef3132f3076443cdb3ef0fcec4d9f Mon Sep 17 00:00:00 2001 From: Meritoo Date: Wed, 28 Aug 2019 15:51:03 +0200 Subject: [PATCH] Add Arrays::containsEmptyStringsOnly() method Returns information if given array contains an empty strings only --- docs/Static-methods/Arrays.md | 23 +++++++++++++++ src/Utilities/Arrays.php | 9 ++++++ tests/Utilities/ArraysTest.php | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/docs/Static-methods/Arrays.md b/docs/Static-methods/Arrays.md index a8c3f14..35547c2 100644 --- a/docs/Static-methods/Arrays.md +++ b/docs/Static-methods/Arrays.md @@ -9,6 +9,29 @@ Common and useful classes, methods, exceptions etc. Class: `Meritoo\Common\Utilities\Arrays` File: `src/Utilities/Arrays.php` +### containsEmptyStringsOnly(array): bool + +> Returns information if given array contains an empty strings only + +##### Arguments + +- `array $array` - The array to verify + +##### Examples + +1) + + - array: `[]` (an empty array) + - result: `false` + +2) + - array: `["", -1]` + - result: `false` + +3) + - array: `["", null, ""]` + - result: `true` + ### getNonEmptyValues(array $values) > Returns non-empty values, e.g. without "" (empty string), null or [] diff --git a/src/Utilities/Arrays.php b/src/Utilities/Arrays.php index 41386c2..b9a9d8e 100644 --- a/src/Utilities/Arrays.php +++ b/src/Utilities/Arrays.php @@ -1633,6 +1633,15 @@ class Arrays return is_array($value) && !empty($value); } + public static function containsEmptyStringsOnly(array $array): bool + { + if (empty($array)) { + return false; + } + + return '' === trim(implode('', $array)); + } + /** * Returns neighbour (next or previous element) for given element * diff --git a/tests/Utilities/ArraysTest.php b/tests/Utilities/ArraysTest.php index e68b25a..82e0c3e 100644 --- a/tests/Utilities/ArraysTest.php +++ b/tests/Utilities/ArraysTest.php @@ -1486,6 +1486,17 @@ letsTest[2] = value_2;'; self::assertSame($expected, Arrays::isNotEmptyArray($value), $description); } + /** + * @param array $array + * @param bool $expected + * + * @dataProvider provideArrayToVerifyIfContainsEmptyStringsOnly + */ + public function testContainsEmptyStringsOnly(array $array, bool $expected): void + { + static::assertSame($expected, Arrays::containsEmptyStringsOnly($array)); + } + /** * Provides simple array to set/replace values with keys * @@ -2870,6 +2881,48 @@ letsTest[2] = value_2;'; ]; } + public function provideArrayToVerifyIfContainsEmptyStringsOnly(): ?Generator + { + yield[ + [], + false, + ]; + + yield[ + [ + '', + 1, + ], + false, + ]; + + yield[ + [ + '', + null, + 1, + ], + false, + ]; + + yield[ + [ + '', + null, + ], + true, + ]; + + yield[ + [ + '', + null, + '', + ], + true, + ]; + } + /** * {@inheritdoc} */