Skip to content

Commit

Permalink
Added reversed() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 24, 2024
1 parent bab88f9 commit b4ec41c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ will return:
<a href="#remove">remove</a>
<a href="#replace">replace</a>
<a href="#reverse">reverse</a>
<a href="#reversed">reversed</a>
<a href="#rsort">rsort</a>
<a href="#rtrim">rtrim</a>
<a href="#search">search</a>
Expand Down Expand Up @@ -365,6 +366,7 @@ will return:
* [ksort()](#ksort) : Sort elements by keys
* [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
* [rsort()](#rsort) : Reverse sort elements using new keys
* [shuffle()](#shuffle) : Randomizes the element order
* [sort()](#sort) : Sorts the elements in-place assigning new keys
Expand Down Expand Up @@ -4211,6 +4213,38 @@ Map::from( ['name' => 'test', 'last' => 'user'] )->reverse();
// ['last' => 'user', 'name' => 'test']
```

**See also:**

* [reversed()](#reversed) - Reverses the element order in a copy of the map


### reversed()

Reverses the element order in a copy of the map.

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

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

The keys are preserved using this method and a new map is created before reversing the elements.
Thus, [reverse()](#reverse) should be preferred for performance reasons if possible.

**Examples:**

```php
Map::from( ['a', 'b'] )->reversed();
// ['b', 'a']

Map::from( ['name' => 'test', 'last' => 'user'] )->reversed();
// ['last' => 'user', 'name' => 'test']
```

**See also:**

* [reverse()](#reverse) - Reverses the element order without returning a new map


### rsort()

Expand Down
24 changes: 24 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -3734,6 +3734,7 @@ public function replace( iterable $elements, bool $recursive = true ) : self
* The keys are preserved using this method.
*
* @return self<int|string,mixed> Updated map for fluid interface
* @see reversed() - Reverses the element order in a copy of the map
*/
public function reverse() : self
{
Expand All @@ -3742,6 +3743,29 @@ public function reverse() : self
}


/**
* Reverses the element order in a copy of the map.
*
* Examples:
* Map::from( ['a', 'b'] )->reversed();
* Map::from( ['name' => 'test', 'last' => 'user'] )->reversed();
*
* Results:
* ['b', 'a']
* ['last' => 'user', 'name' => 'test']
*
* The keys are preserved using this method and a new map is created before reversing the elements.
* Thus, reverse() should be preferred for performance reasons if possible.
*
* @return self<int|string,mixed> New map with a reversed copy of the elements
* @see reverse() - Reverses the element order with keys without returning a new map
*/
public function reversed() : self
{
return ( clone $this )->reverse();
}


/**
* Sorts all elements in reverse order using new keys.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,17 @@ public function testReverseKeys()
}


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

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


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

0 comments on commit b4ec41c

Please sign in to comment.