From 292c5e6d4f028263946b00caf312efb8bf739fed Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 22 Feb 2019 12:49:12 +0100 Subject: [PATCH] Arrays > getNonEmptyValues() method > returns non-empty values, e.g. without "" (empty string), null or [] Arrays > getNonEmptyValuesAsString() method > returns non-empty values concatenated by given separator --- README.md | 3 +- docs/Base-test-case.md | 3 +- docs/Collection-of-elements.md | 3 +- docs/Exceptions.md | 3 +- docs/Static-methods.md | 3 +- docs/Static-methods/Arrays.md | 78 ++++++ docs/Static-methods/Regex.md | 7 +- docs/Value-Objects.md | 3 +- src/Utilities/Arrays.php | 48 +++- tests/Utilities/Arrays/SimpleToString.php | 44 ++++ tests/Utilities/ArraysTest.php | 296 ++++++++++++++++++++++ 11 files changed, 481 insertions(+), 10 deletions(-) create mode 100644 docs/Static-methods/Arrays.md create mode 100644 tests/Utilities/Arrays/SimpleToString.php diff --git a/README.md b/README.md index ed6c735..b6dd475 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ composer require meritoo/common-library 2. [Collection of elements](docs/Collection-of-elements.md) 3. [Exceptions](docs/Static-methods.md) 4. [Static methods](docs/Static-methods.md) - 1. [Regex](docs/Static-methods/Regex.md) + 1. [Arrays](docs/Static-methods/Arrays.md) + 2. [Regex](docs/Static-methods/Regex.md) 5. [Value Objects](docs/Value-Objects.md) # Development diff --git a/docs/Base-test-case.md b/docs/Base-test-case.md index aad3b0d..b35c744 100644 --- a/docs/Base-test-case.md +++ b/docs/Base-test-case.md @@ -48,7 +48,8 @@ class MimeTypesTest extends BaseTestCase 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) - 1. [Regex](Static-methods/Regex.md) + 1. [Arrays](Static-methods/Arrays.md) + 2. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Collection-of-elements.md b/docs/Collection-of-elements.md index 40e366f..2493afc 100644 --- a/docs/Collection-of-elements.md +++ b/docs/Collection-of-elements.md @@ -46,7 +46,8 @@ var_dump($simpleCollection->has('dolor')); // bool(true) 2. [**Collection of elements**](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) - 1. [Regex](Static-methods/Regex.md) + 1. [Arrays](Static-methods/Arrays.md) + 2. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Exceptions.md b/docs/Exceptions.md index 29709c3..14d7021 100644 --- a/docs/Exceptions.md +++ b/docs/Exceptions.md @@ -57,7 +57,8 @@ class UnknownSimpleTypeException extends UnknownTypeException 2. [Collection of elements](Collection-of-elements.md) 3. [**Exceptions**](Exceptions.md) 4. [Static methods](Static-methods.md) - 1. [Regex](Static-methods/Regex.md) + 1. [Arrays](Static-methods/Arrays.md) + 2. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Static-methods.md b/docs/Static-methods.md index 29fb42c..d1d2227 100644 --- a/docs/Static-methods.md +++ b/docs/Static-methods.md @@ -19,7 +19,8 @@ var_dump($firstElement); // string(5) "lorem" 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [**Static methods**](Static-methods.md) - 1. [Regex](Static-methods/Regex.md) + 1. [Arrays](Static-methods/Arrays.md) + 2. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Static-methods/Arrays.md b/docs/Static-methods/Arrays.md new file mode 100644 index 0000000..b798505 --- /dev/null +++ b/docs/Static-methods/Arrays.md @@ -0,0 +1,78 @@ +# Meritoo Common Library + +Common and useful classes, methods, exceptions etc. + +# Arrays + +> Useful methods related to arrays + +Class: `Meritoo\Common\Utilities\Arrays` +File: `src/Utilities/Arrays.php` + +### getNonEmptyValues(array $values) + +> Returns non-empty values, e.g. without "" (empty string), null or [] + +##### Arguments + +- `array $values` - The values to filter + +##### Example 1 + +- values: `[]` (no values) +- result: `[]` (an empty array) + +##### Example 2 + +- values: `[null, ""]` (all empty values) +- result: `[]` (an empty array) + +##### Example 3 + +- values: `["test 1", "", 123, null, 0]` +- result: `["test 1", 123, 0]` + +### getNonEmptyValuesAsString(array $values, $separator = ', ') + +> Returns non-empty values concatenated by given separator + +##### Arguments + +- `array $values` - The values to filter +- `[string $separator]` - (optional) Separator used to implode the values. Default: ", ". + +##### Example 1 + +- values: `[]` (no values) +- separator: default or any other string +- result: `""` (an empty string) + +##### Example 2 + +- values: `[null, ""]` (all empty values) +- separator: default or any other string +- result: `""` (an empty string) + +##### Example 3 + +- values: `["test 1", "", 123, null, 0]` +- separator: `", "` (default) +- result: `"test 1, 123, 0"` + +##### Example 4 + +- values: `["test 1", "", 123, null, 0]` +- separator: `" | "` +- result: `"test 1 | 123 | 0"` + +# More + +1. [Base test case (with common methods and data providers)](../Base-test-case.md) +2. [Collection of elements](../Collection-of-elements.md) +3. [Exceptions](../Exceptions.md) +4. [Static methods](../Static-methods.md) + 1. [**Arrays**](Arrays.md) + 2. [Regex](Regex.md) +5. [Value Objects](../Value-Objects.md) + +[‹ Back to `Readme`](../../README.md) diff --git a/docs/Static-methods/Regex.md b/docs/Static-methods/Regex.md index 8358534..e4ec57c 100644 --- a/docs/Static-methods/Regex.md +++ b/docs/Static-methods/Regex.md @@ -4,14 +4,14 @@ Common and useful classes, methods, exceptions etc. # Regex -*Useful methods related to regular expressions* +> Useful methods related to regular expressions Class: `Meritoo\Common\Utilities\Regex` File: `src/Utilities/Regex.php` ### createSlug($value) -*Returns slug for given value* +> Returns slug for given value ##### Arguments @@ -38,7 +38,8 @@ File: `src/Utilities/Regex.php` 2. [Collection of elements](../Collection-of-elements.md) 3. [Exceptions](../Exceptions.md) 4. [Static methods](../Static-methods.md) - 1. [**Regex**](../Static-methods/Regex.md) + 1. [Arrays](../Static-methods/Arrays.md) + 2. [**Regex**](Regex.md) 5. [Value Objects](../Value-Objects.md) [‹ Back to `Readme`](../../README.md) diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index 9aa0325..8497655 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -48,7 +48,8 @@ New instance can be created using: 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) - 1. [Regex](Static-methods/Regex.md) + 1. [Arrays](Static-methods/Arrays.md) + 2. [Regex](Static-methods/Regex.md) 5. [**Value Objects**](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/src/Utilities/Arrays.php b/src/Utilities/Arrays.php index 3df6daa..7bce3f2 100644 --- a/src/Utilities/Arrays.php +++ b/src/Utilities/Arrays.php @@ -9,7 +9,7 @@ namespace Meritoo\Common\Utilities; /** - * Useful arrays methods + * Useful methods related to arrays * * @author Meritoo * @copyright Meritoo @@ -1573,6 +1573,52 @@ class Arrays return $dimensionsCount; } + /** + * Returns non-empty values, e.g. without "" (empty string), null or [] + * + * @param array $values The values to filter + * @return array + */ + public static function getNonEmptyValues(array $values) + { + /* + * No values? + * Nothing to do + */ + if (empty($values)) { + return []; + } + + return array_filter($values, function ($value) { + $nonEmptyScalar = is_scalar($value) && '' !== $value; + $nonEmptyArray = is_array($value) && !empty($value); + + return $nonEmptyScalar || $nonEmptyArray || is_object($value); + }); + } + + /** + * Returns non-empty values concatenated by given separator + * + * @param array $values The values to filter + * @param string $separator (optional) Separator used to implode the values. Default: ", ". + * @return string + */ + public static function getNonEmptyValuesAsString(array $values, $separator = ', ') + { + $nonEmpty = self::getNonEmptyValues($values); + + /* + * No values? + * Nothing to do + */ + if (empty($nonEmpty)) { + return ''; + } + + return implode($separator, $nonEmpty); + } + /** * Returns neighbour (next or previous element) for given element * diff --git a/tests/Utilities/Arrays/SimpleToString.php b/tests/Utilities/Arrays/SimpleToString.php new file mode 100644 index 0000000..04bec45 --- /dev/null +++ b/tests/Utilities/Arrays/SimpleToString.php @@ -0,0 +1,44 @@ + + * @copyright Meritoo + */ +class SimpleToString +{ + /** + * Identifier + * + * @var string + */ + private $id; + + /** + * Class constructor + * + * @param string $id Identifier + */ + public function __construct($id) + { + $this->id = $id; + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return sprintf('Instance with ID: %s', $this->id); + } +} diff --git a/tests/Utilities/ArraysTest.php b/tests/Utilities/ArraysTest.php index d43da0d..71d9583 100644 --- a/tests/Utilities/ArraysTest.php +++ b/tests/Utilities/ArraysTest.php @@ -9,6 +9,7 @@ namespace Meritoo\Common\Test\Utilities; use Meritoo\Common\Test\Base\BaseTestCase; +use Meritoo\Common\Test\Utilities\Arrays\SimpleToString; use Meritoo\Common\Utilities\Arrays; /** @@ -1486,6 +1487,43 @@ letsTest[2] = value_2;'; self::assertTrue(Arrays::isMultiDimensional($this->complexArray)); } + /** + * @param string $description Description of test case + * @param array $values The values to filter + * @param array $expected Expected non-empty values + * + * @dataProvider provideValuesToFilterNonEmpty + */ + public function testGetNonEmptyValues($description, array $values, array $expected) + { + self::assertSame($expected, Arrays::getNonEmptyValues($values), $description); + } + + /** + * @param string $description Description of test case + * @param array $values The values to filter + * @param string $expected Expected non-empty values (as string) + * + * @dataProvider provideValuesToFilterNonEmptyAsStringUsingDefaultSeparator + */ + public function testGetNonEmptyValuesAsStringUsingDefaultSeparator($description, array $values, $expected) + { + self::assertSame($expected, Arrays::getNonEmptyValuesAsString($values), $description); + } + + /** + * @param string $description Description of test case + * @param array $values The values to filter + * @param string $separator Separator used to implode the values + * @param string $expected Expected non-empty values (as string) + * + * @dataProvider provideValuesToFilterNonEmptyAsString + */ + public function testGetNonEmptyValuesAsString($description, array $values, $separator, $expected) + { + self::assertSame($expected, Arrays::getNonEmptyValuesAsString($values, $separator), $description); + } + /** * Provides simple array to set/replace values with keys * @@ -1807,6 +1845,264 @@ letsTest[2] = value_2;'; ]; } + /** + * Provide values to filter and get non-empty values + * + * @return \Generator + */ + public function provideValuesToFilterNonEmpty() + { + $simpleObject = new SimpleToString('1234'); + + yield[ + 'An empty array (no values to filter)', + [], + [], + ]; + + yield[ + 'All values are empty', + [ + '', + null, + [], + ], + [], + ]; + + yield[ + '5 values with 2 empty strings', + [ + 'test 1', + '', + 'test 2', + 'test 3', + '', + ], + [ + 0 => 'test 1', + 2 => 'test 2', + 3 => 'test 3', + ], + ]; + + yield[ + '"0" shouldn\'t be treated like an empty value', + [ + 123, + 0, + 456, + ], + [ + 123, + 0, + 456, + ], + ]; + + yield[ + 'Object shouldn\'t be treated like an empty value', + [ + 'test 1', + $simpleObject, + 'test 2', + null, + 'test 3', + ], + [ + 0 => 'test 1', + 1 => $simpleObject, + 2 => 'test 2', + 4 => 'test 3', + ], + ]; + + yield[ + 'Mixed values (non-empty, empty, strings, integers, objects)', + [ + 'test 1', + '', + 123, + null, + 'test 2', + 'test 3', + 0, + $simpleObject, + 456, + [], + $simpleObject, + ], + [ + 0 => 'test 1', + 2 => 123, + 4 => 'test 2', + 5 => 'test 3', + 6 => 0, + 7 => $simpleObject, + 8 => 456, + 10 => $simpleObject, + ], + ]; + } + + /** + * Provide values to filter and get non-empty values concatenated by default separator + * + * @return \Generator + */ + public function provideValuesToFilterNonEmptyAsStringUsingDefaultSeparator() + { + yield[ + 'An empty array (no values to filter)', + [], + '', + ]; + + yield[ + 'All values are empty', + [ + '', + null, + [], + ], + '', + ]; + + yield[ + '5 values with 2 empty strings', + [ + 'test 1', + '', + 'test 2', + 'test 3', + '', + ], + 'test 1, test 2, test 3', + ]; + + yield[ + 'Numbers with "0" that shouldn\'t be treated like an empty value', + [ + 123, + 0, + 456, + ], + '123, 0, 456', + ]; + + yield[ + 'Object shouldn\'t be treated like an empty value', + [ + 'test 1', + new SimpleToString('1234'), + 'test 2', + null, + 'test 3', + ], + 'test 1, Instance with ID: 1234, test 2, test 3', + ]; + + yield[ + 'Mixed values (non-empty, empty, strings, integers, objects)', + [ + 'test 1', + '', + 123, + null, + 'test 2', + 'test 3', + 0, + new SimpleToString('A1XC90Z'), + 456, + [], + new SimpleToString('FF-45-0Z'), + ], + 'test 1, 123, test 2, test 3, 0, Instance with ID: A1XC90Z, 456, Instance with ID: FF-45-0Z', + ]; + } + + /** + * Provide values to filter and get non-empty values concatenated by given separator + * + * @return \Generator + */ + public function provideValuesToFilterNonEmptyAsString() + { + yield[ + 'An empty array (no values to filter)', + [], + ' | ', + '', + ]; + + yield[ + 'All values are empty', + [ + '', + null, + [], + ], + ' | ', + '', + ]; + + yield[ + '5 values with 2 empty strings', + [ + 'test 1', + '', + 'test 2', + 'test 3', + '', + ], + ' | ', + 'test 1 | test 2 | test 3', + ]; + + yield[ + 'Numbers with "0" that shouldn\'t be treated like an empty value', + [ + 123, + 0, + 456, + ], + ' <-> ', + '123 <-> 0 <-> 456', + ]; + + yield[ + 'Object shouldn\'t be treated like an empty value', + [ + 'test 1', + new SimpleToString('1234'), + 'test 2', + null, + 'test 3', + ], + ' | ', + 'test 1 | Instance with ID: 1234 | test 2 | test 3', + ]; + + yield[ + 'Mixed values (non-empty, empty, strings, integers, objects)', + [ + 'test 1', + '', + 123, + null, + 'test 2', + 'test 3', + 0, + new SimpleToString('A1XC90Z'), + 456, + [], + new SimpleToString('FF-45-0Z'), + ], + ';', + 'test 1;123;test 2;test 3;0;Instance with ID: A1XC90Z;456;Instance with ID: FF-45-0Z', + ]; + } + /** * {@inheritdoc} */