Skip to content

Commit

Permalink
Added toReversed() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 24, 2024
1 parent b4ec41c commit 7e4ffd6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ will return:
<a href="#times">times</a>
<a href="#toarray">toArray</a>
<a href="#tojson">toJson</a>
<a href="#toreversed">toReversed</a>
<a href="#tosorted">toSorted</a>
<a href="#tourl">toUrl</a>
<a href="#transform">transform</a>
Expand Down Expand Up @@ -367,11 +368,12 @@ will return:
* [order()](#order) : Orders elements by the passed keys
* [reverse()](#reverse) : Reverses the array order preserving keys
* [reversed()](#reversed) : Reverses the element order in a copy of the map
* [toReversed()](#toreversed) : Reverses the element order in a copy of the map (alias)
* [rsort()](#rsort) : Reverse sort elements using new keys
* [shuffle()](#shuffle) : Randomizes the element order
* [sort()](#sort) : Sorts the elements in-place assigning new keys
* [sorted()](#sorted) : Sorts the elements in a copy of the map using new keys
* [toSorted()](#toSorted) : Sorts the elements in a copy of the map using new keys (alias)
* [toSorted()](#tosorted) : Sorts the elements in a copy of the map using new keys (alias)
* [uasort()](#uasort) : Sorts elements preserving keys using callback
* [uksort()](#uksort) : Sorts elements by keys using callback
* [usort()](#usort) : Sorts elements using callback assigning new keys
Expand Down Expand Up @@ -5421,6 +5423,24 @@ Map::from( ['a', 'b'] )->toJson( JSON_FORCE_OBJECT );
```


### toReversed()

Reverses the element order in a copy of the map (alias).

```php
public function toReversed() : self
```

* @return **self&#60;int&#124;string,mixed&#62;** New map with a reversed copy of the elements

This method is an alias for [reversed()](#reversed). For performance reasons, reversed() should be
preferred because it uses one method call less than toReversed().

**See also:**

* [reversed()](#reversed) - Underlying method with same parameters and return value but better performance


### toSorted()

Sorts the elements in a copy of the map using new keys (alias).
Expand Down
17 changes: 16 additions & 1 deletion src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4931,7 +4931,22 @@ public function toJson( int $options = 0 ) : ?string


/**
* Sorts the elements in a copy of the map using new keys.
* Reverses the element order in a copy of the map (alias).
*
* This method is an alias for reversed(). For performance reasons, reversed() should be
* preferred because it uses one method call less than toReversed().
*
* @return self<int|string,mixed> New map with a reversed copy of the elements
* @see reversed() - Underlying method with same parameters and return value but better performance
*/
public function toReversed() : self
{
return $this->reversed();
}


/**
* Sorts the elements in a copy of the map using new keys (alias).
*
* This method is an alias for sorted(). For performance reasons, sorted() should be
* preferred because it uses one method call less than toSorted().
Expand Down
11 changes: 11 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3226,6 +3226,17 @@ public function testTimesObjects()
}


public function testToReversed()
{
$m = new Map( ['hello', 'world'] );
$r = $m->toReversed();

$this->assertNotSame( $r, $m );
$this->assertInstanceOf( Map::class, $r );
$this->assertSame( [1 => 'world', 0 => 'hello'], $r->toArray() );
}


public function testToSorted()
{
$m = new Map( [-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4] );
Expand Down

0 comments on commit 7e4ffd6

Please sign in to comment.