Skip to content

Commit

Permalink
Implemented insertBefore() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Sep 24, 2021
1 parent bd1bd89 commit dc159c7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,34 @@ Map::from( ['foo', 'bar'] )->insertAfter( null, 'baz' );
```


### insertBefore()

Inserts the value or values before the given element.

```php
public function insertBefore( $element, $value ) : self
```

* @param mixed `$element` Element before the value is inserted
* @param mixed `$value` Element or list of elements to insert
* @return self Updated map for fluid interface

Numerical array indexes are not preserved.

**Examples:**

```php
Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertBefore( 'bar', 'baz' );
// ['a' => 'foo', 0 => 'baz', 'b' => 'bar']

Map::from( ['foo', 'bar'] )->insertBefore( 'bar', ['baz', 'boo'] );
// ['foo', 'baz', 'boo', 'bar']

Map::from( ['foo', 'bar'] )->insertBefore( null, 'baz' );
// ['foo', 'bar', 'baz']
```


### intersect()

Returns all values in a new map that are available in both, the map and the given elements.
Expand Down
28 changes: 28 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,34 @@ public function insertAfter( $element, $value ) : self
}


/**
* Inserts the value or values before the given element.
*
* Examples:
* Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertBefore( 'bar', 'baz' );
* Map::from( ['foo', 'bar'] )->insertBefore( 'bar', ['baz', 'boo'] );
* Map::from( ['foo', 'bar'] )->insertBefore( null, 'baz' );
*
* Results:
* ['a' => 'foo', 0 => 'baz', 'b' => 'bar']
* ['foo', 'baz', 'boo', 'bar']
* ['foo', 'bar', 'baz']
*
* Numerical array indexes are not preserved.
*
* @param mixed $element Element before the value is inserted
* @param mixed $value Element or list of elements to insert
* @return self Updated map for fluid interface
*/
public function insertBefore( $element, $value ) : self
{
$pos = ( $element !== null ? $this->pos( $element ) : count( $this->list ) );
array_splice( $this->list, $pos, 0, $this->getArray( $value ) );

return $this;
}


/**
* Returns all values in a new map that are available in both, the map and the given elements.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,33 @@ public function testInsertAfterEnd()
}


public function testInsertBefore()
{
$r = Map::from( ['a' => 'foo', 'b' => 'bar'] )->insertBefore( 'bar', 'baz' );

$this->assertInstanceOf( Map::class, $r );
$this->assertEquals( ['a' => 'foo', 0 => 'baz', 'b' => 'bar'], $r->toArray() );
}


public function testInsertBeforeArray()
{
$r = Map::from( ['foo', 'bar'] )->insertBefore( 'bar', ['baz', 'boo'] );

$this->assertInstanceOf( Map::class, $r );
$this->assertEquals( ['foo', 'baz', 'boo', 'bar'], $r->toArray() );
}


public function testInsertBeforeEnd()
{
$r = Map::from( ['foo', 'bar'] )->insertBefore( null, 'baz' );

$this->assertInstanceOf( Map::class, $r );
$this->assertEquals( ['foo', 'bar', 'baz'], $r->toArray() );
}


public function testIntersect()
{
$m = new Map( ['id' => 1, 'first_word' => 'Hello'] );
Expand Down

0 comments on commit dc159c7

Please sign in to comment.