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
This commit is contained in:
Meritoo
2019-02-22 12:49:12 +01:00
parent 79c09a26a6
commit 292c5e6d4f
11 changed files with 481 additions and 10 deletions

View File

@@ -20,7 +20,8 @@ composer require meritoo/common-library
2. [Collection of elements](docs/Collection-of-elements.md) 2. [Collection of elements](docs/Collection-of-elements.md)
3. [Exceptions](docs/Static-methods.md) 3. [Exceptions](docs/Static-methods.md)
4. [Static methods](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) 5. [Value Objects](docs/Value-Objects.md)
# Development # Development

View File

@@ -48,7 +48,8 @@ class MimeTypesTest extends BaseTestCase
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.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) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -46,7 +46,8 @@ var_dump($simpleCollection->has('dolor')); // bool(true)
2. [**Collection of elements**](Collection-of-elements.md) 2. [**Collection of elements**](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.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) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -57,7 +57,8 @@ class UnknownSimpleTypeException extends UnknownTypeException
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [**Exceptions**](Exceptions.md) 3. [**Exceptions**](Exceptions.md)
4. [Static methods](Static-methods.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) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -19,7 +19,8 @@ var_dump($firstElement); // string(5) "lorem"
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [**Static methods**](Static-methods.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) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -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)

View File

@@ -4,14 +4,14 @@ Common and useful classes, methods, exceptions etc.
# Regex # Regex
*Useful methods related to regular expressions* > Useful methods related to regular expressions
Class: `Meritoo\Common\Utilities\Regex` Class: `Meritoo\Common\Utilities\Regex`
File: `src/Utilities/Regex.php` File: `src/Utilities/Regex.php`
### createSlug($value) ### createSlug($value)
*Returns slug for given value* > Returns slug for given value
##### Arguments ##### Arguments
@@ -38,7 +38,8 @@ File: `src/Utilities/Regex.php`
2. [Collection of elements](../Collection-of-elements.md) 2. [Collection of elements](../Collection-of-elements.md)
3. [Exceptions](../Exceptions.md) 3. [Exceptions](../Exceptions.md)
4. [Static methods](../Static-methods.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) 5. [Value Objects](../Value-Objects.md)
[‹ Back to `Readme`](../../README.md) [‹ Back to `Readme`](../../README.md)

View File

@@ -48,7 +48,8 @@ New instance can be created using:
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.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) 5. [**Value Objects**](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -9,7 +9,7 @@
namespace Meritoo\Common\Utilities; namespace Meritoo\Common\Utilities;
/** /**
* Useful arrays methods * Useful methods related to arrays
* *
* @author Meritoo <github@meritoo.pl> * @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl> * @copyright Meritoo <http://www.meritoo.pl>
@@ -1573,6 +1573,52 @@ class Arrays
return $dimensionsCount; 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 * Returns neighbour (next or previous element) for given element
* *

View File

@@ -0,0 +1,44 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Meritoo\Common\Test\Utilities\Arrays;
/**
* Simple class convertible to string.
* Used for testing the Arrays class.
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*/
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);
}
}

View File

@@ -9,6 +9,7 @@
namespace Meritoo\Common\Test\Utilities; namespace Meritoo\Common\Test\Utilities;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Test\Utilities\Arrays\SimpleToString;
use Meritoo\Common\Utilities\Arrays; use Meritoo\Common\Utilities\Arrays;
/** /**
@@ -1486,6 +1487,43 @@ letsTest[2] = value_2;';
self::assertTrue(Arrays::isMultiDimensional($this->complexArray)); 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 * 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} * {@inheritdoc}
*/ */