Skip to content

Commit

Permalink
feat: update all() function
Browse files Browse the repository at this point in the history
update `all()` function to be more useful by making the second param a callback.
  • Loading branch information
lots0logs committed May 17, 2021
1 parent 7e197e4 commit 2595f70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Utilities that make reading and writing PHP code more convenient and enjoyable",
"type": "library",
"license": "GPL-v2-or-later",
"version": "1.2.0",
"version": "1.2.1",
"authors": [
{
"name": "Elegant Themes",
Expand Down
14 changes: 7 additions & 7 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,21 @@ public function _arraySortByCallback( $a, $b ): int {

/**
* Returns `true` if all values in `$array` are not empty, `false` otherwise.
* If `$condition` is provided then values are checked against it instead of `empty()`.
* If `$callback` is provided then values are passed to it instead of {@see empty()} (it should return a booleon value).
*
* @param array $array
* @param bool $condition Compare values to this instead of `empty()`. Optional.
* @param array $array
* @param callable $callback Pass each value to callback instead of {@see empty()}. Optional.
*/
public function all( array $array, $condition = null ): bool {
if ( null === $condition ) {
public function all( array $array, $callback = null ): bool {
if ( null === $callback ) {
foreach( $array as $key => $value ) {
if ( empty( $value ) ) {
return false;
}
}
} else {
} else if ( is_callable( $callback ) ) {
foreach( $array as $key => $value ) {
if ( $value !== $condition ) {
if ( ! $callback( $value ) ) {
return false;
}
}
Expand Down

0 comments on commit 2595f70

Please sign in to comment.