mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Add Arrays::containsEmptyStringsOnly() method
Returns information if given array contains an empty strings only
This commit is contained in:
@@ -9,6 +9,29 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
Class: `Meritoo\Common\Utilities\Arrays`
|
Class: `Meritoo\Common\Utilities\Arrays`
|
||||||
File: `src/Utilities/Arrays.php`
|
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)
|
### getNonEmptyValues(array $values)
|
||||||
|
|
||||||
> Returns non-empty values, e.g. without "" (empty string), null or []
|
> Returns non-empty values, e.g. without "" (empty string), null or []
|
||||||
|
|||||||
@@ -1633,6 +1633,15 @@ class Arrays
|
|||||||
return is_array($value) && !empty($value);
|
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
|
* Returns neighbour (next or previous element) for given element
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1486,6 +1486,17 @@ letsTest[2] = value_2;';
|
|||||||
self::assertSame($expected, Arrays::isNotEmptyArray($value), $description);
|
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
|
* 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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user