Skip to content

Commit

Permalink
Added any() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 30, 2024
1 parent 8f08be6 commit 75b6ef4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ will return:
* [__call()](#__call) : Calls a custom method
* [__callStatic()](#__callstatic) : Calls a custom method statically
* [all()](#all) : Returns the plain array
* [any()](#any) : Tests if at least one element satisfies the callback function
* [at()](#at) : Returns the value at the given position
* [bool()](#bool) : Returns an element by key and casts it to boolean
* [call()](#call) : Calls the given method on all items
Expand Down Expand Up @@ -425,6 +426,7 @@ will return:
### Test

* [function is_map()](#is_map-function) : Tests if the variable is a map object
* [any()](#any) : Tests if at least one element satisfies the callback function
* [compare()](#compare) : Compares the value against all map elements
* [contains()](#contains) : Tests if an item exists in the map
* [each()](#each) : Applies a callback to each element
Expand Down Expand Up @@ -727,6 +729,32 @@ Map::from( ['a'] )->all();
```


### any()

Tests if at least one element satisfies the callback function.

```php
public function any( \Closure $callback ) : bool
```

* @param \Closure $callback Anonymous function with (item, key) parameter
* @return bool TRUE if at least one element satisfies the callback function, FALSE if not

**Examples:**

```php
Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return $item === 'a';
} );
// TRUE

Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return !is_string( $item );
} );
// FALSE
```


### arsort()

Sorts all elements in reverse order and maintains the key association.
Expand Down
36 changes: 35 additions & 1 deletion src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,40 @@ public function all() : array
}


/**
* Tests if at least one element satisfies the callback function.
*
* Examples:
* Map::from( ['a', 'b'] )->any( function( $item, $key ) {
* return $item === 'a';
* } );
* Map::from( ['a', 'b'] )->any( function( $item, $key ) {
* return !is_string( $item );
* } );
*
* Results:
* The first example will return TRUE while the last one will return FALSE
*
* @param \Closure $callback Anonymous function with (item, key) parameter
* @return bool TRUE if at least one element satisfies the callback function, FALSE if not
*/
public function any( \Closure $callback ) : bool
{
if( function_exists( 'array_any' ) ) {
return array_any( $this->list(), $callback );
}

foreach( $this->list() as $key => $item )
{
if( $callback( $item, $key ) ) {
return true;
}
}

return false;
}


/**
* Sorts all elements in reverse order and maintains the key association.
*
Expand Down Expand Up @@ -4246,7 +4280,7 @@ public function slice( int $offset, ?int $length = null ) : self
* Map::from( ['a', 'b'] )->some( 'a' );
* Map::from( ['a', 'b'] )->some( ['a', 'c'] );
* Map::from( ['a', 'b'] )->some( function( $item, $key ) {
* return $item === 'a'
* return $item === 'a';
* } );
* Map::from( ['a', 'b'] )->some( ['c', 'd'] );
* Map::from( ['1', '2'] )->some( [2], true );
Expand Down
14 changes: 14 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public function testAll()
}


public function testAny()
{
$result = Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return $item === 'a';
} );
$this->assertTrue( $result );

$result = Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return !is_string( $item );
} );
$this->assertFalse( $result );
}


public function testArsortNummeric()
{
$m = ( new Map( [1 => -3, 2 => -2, 3 => -4, 4 => -1, 5 => 0, 6 => 4, 7 => 3, 8 => 1, 9 => 2] ) )->arsort();
Expand Down

0 comments on commit 75b6ef4

Please sign in to comment.