Skip to content

Commit

Permalink
Added firstKey() and lastKey() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Feb 14, 2020
1 parent e15e262 commit efe9443
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -118,6 +118,7 @@ will return:
* [filter()](#filter) : Applies a filter to the map elements
* [find()](#find) : Returns the first matching element
* [first()](#first) : Returns the first element
* [firstKey()](#firstkey) : Returns the first key
* [flip()](#flip) : Exchanges keys with their values
* [flat()](#flat) : Flattens multi-dimensional elements
* [from()](#from) : Creates a new map from passed elements
Expand All @@ -135,6 +136,7 @@ will return:
* [krsort()](#krsort) : Reverse sort elements by keys
* [ksort()](#ksort) : Sort elements by keys
* [last()](#last) : Returns the last element
* [lastKey()](#lastkey) : Returns the last key
* [map()](#map) : Applies a callback to each element and returns the results
* [merge()](#merge) : Combines elements overwriting existing ones
* [method()](#method) : Registers a custom method
Expand Down Expand Up @@ -806,6 +808,28 @@ The first example will return 'a' and the second one 'x'. The third example
will throw the exception passed if the map contains no elements.


### firstKey()

Returns the first key from the map.

```php
public function firstKey()
```

* @return mixed First key of map or `NULL` if empty

**Examples:**

```php
Map::from( ['a' => 1, 'b' => 2] )->lastKey();
Map::from( [] )->lastKey();
```

**Results:**

The first example will return 'a' and the second one `NULL`.


### flat()

Creates a new map with all sub-array elements added recursively
Expand Down Expand Up @@ -1312,6 +1336,28 @@ The first example will return 'b' and the second one 'x'. The third example
will throw the exception passed if the map contains no elements.


### lastKey()

Returns the last key from the map.

```php
public function lastKey()
```

* @return mixed Last key of map or `NULL` if empty

**Examples:**

```php
Map::from( ['a' => 1, 'b' => 2] )->lastKey();
Map::from( [] )->lastKey();
```

**Results:**

The first example will return 'b' and the second one `NULL`.


### map()

Calls the passed function once for each element and returns a new map for the result.
Expand Down
48 changes: 46 additions & 2 deletions src/Map.php
Expand Up @@ -659,8 +659,6 @@ public function find( \Closure $callback, bool $last = false )
* will throw the exception passed if the map contains no elements.
*
* @param mixed $default Default value or exception if the map contains no elements
*
* @param mixed $default Default value if map is empty
* @return mixed First value of map or default value
*/
public function first( $default = null )
Expand All @@ -677,6 +675,29 @@ public function first( $default = null )
}


/**
* Returns the first key from the map.
*
* Examples:
* Map::from( ['a' => 1, 'b' => 2] )->firstKey();
* Map::from( [] )->firstKey();
*
* Results:
* The first example will return 'a' and the second one NULL.
*
* @return mixed First key of map or NULL if empty
*/
public function firstKey()
{
if( function_exists( 'array_key_first' ) ) {
return array_key_first( $this->list );
}

reset( $this->list );
return key( $this->list);
}


/**
* Creates a new map with all sub-array elements added recursively
*
Expand Down Expand Up @@ -1147,6 +1168,29 @@ public function last( $default = null )
}


/**
* Returns the last key from the map.
*
* Examples:
* Map::from( ['a' => 1, 'b' => 2] )->lastKey();
* Map::from( [] )->lastKey();
*
* Results:
* The first example will return 'b' and the second one NULL.
*
* @return mixed Last key of map or NULL if empty
*/
public function lastKey()
{
if( function_exists( 'array_key_last' ) ) {
return array_key_last( $this->list );
}

end( $this->list );
return key( $this->list );
}


/**
* Calls the passed function once for each element and returns a new map for the result.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/MapTest.php
Expand Up @@ -528,6 +528,18 @@ public function testFirstWithException()
}


public function testFirstKey()
{
$this->assertEquals( 'a', Map::from( ['a' => 1, 'b' => 2] )->firstKey() );
}


public function testFirstKeyEmpty()
{
$this->assertEquals( null, Map::from( [] )->firstKey() );
}


public function testFlat()
{
$m = Map::from( [[0, 1], [2, 3]] );
Expand Down Expand Up @@ -828,6 +840,18 @@ public function testLastWithException()
}


public function testLastKey()
{
$this->assertEquals( 'b', Map::from( ['a' => 1, 'b' => 2] )->lastKey() );
}


public function testLastKeyEmpty()
{
$this->assertEquals( null, Map::from( [] )->lastKey() );
}


public function testMap()
{
$m = new Map( ['first' => 'test', 'last' => 'user'] );
Expand Down

0 comments on commit efe9443

Please sign in to comment.