From 9e1cb1a56e1190ad8df174e1a21976f42eb95e29 Mon Sep 17 00:00:00 2001 From: Aimeos Date: Sat, 3 Jun 2023 16:28:24 +0200 Subject: [PATCH 1/2] Implemented with() method similar to Javascript --- README.md | 33 +++++++++++++++++++++++++++++++++ src/Map.php | 27 ++++++++++++++++++++++++++- tests/MapTest.php | 11 +++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 17cbf88..485196b 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,7 @@ will return: values walk where +with zip @@ -328,6 +329,7 @@ will return: * [set()](#set) : Overwrites or adds an element * [union()](#union) : Adds the elements without overwriting existing ones * [unshift()](#unshift) : Adds an element at the beginning +* [with()](#with) : Returns a copy and sets an element ### Aggregate @@ -5814,6 +5816,37 @@ Map::from( [ ``` +### with() + +Returns a copy of the map with the element at the given index replaced with the given value. + +```php +public function with( $key, $value ) : self +``` + +* @param **int|string** `$key` Array key to set or replace +* @param **mixed** `$value` New value for the given key +* @return **self<int|string,mixed>** New map of the values + +The original map stays untouched! This method is a shortcut for calling the +[copy()](#copy) and [set()](#set) methods. + +**Examples:** + +```php +$m = Map::from( ['a' => 1] ); + +$m->with( 2, 'b' ); +// ['a' => 1, 2 => 'b'] + +$m->with( 'a', 2 ); +// ['a' => 2] + +$m->all(); +// ['a' => 1] +``` + + ### zip() Merges the values of all arrays at the corresponding index. diff --git a/src/Map.php b/src/Map.php index 450a717..e97c4a8 100644 --- a/src/Map.php +++ b/src/Map.php @@ -5359,13 +5359,38 @@ public function where( string $key, string $op, $value ) : self } + /** + * Returns a copy of the map with the element at the given index replaced with the given value. + * + * Examples: + * $m = Map::from( ['a' => 1] ); + * $m->with( 2, 'b' ); + * $m->with( 'a', 2 ); + * + * Results: + * ['a' => 1, 2 => 'b'] + * ['a' => 2] + * + * The original map ($m) stays untouched! + * This method is a shortcut for calling the copy() and set() methods. + * + * @param int|string $key Array key to set or replace + * @param mixed $value New value for the given key + * @return self New map + */ + public function with( $key, $value ) : self + { + return $this->copy()->set( $key, $value ); + } + + /** * Merges the values of all arrays at the corresponding index. * * Examples: * $en = ['one', 'two', 'three']; * $es = ['uno', 'dos', 'tres']; - * $m = new Map( [1, 2, 3] )->zip( $en, $es ); + * $m = Map::from( [1, 2, 3] )->zip( $en, $es ); * * Results: * [ diff --git a/tests/MapTest.php b/tests/MapTest.php index 21685d2..cd2c71e 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -3527,6 +3527,17 @@ public function testWherePath() } + public function testWith() + { + $m = Map::from( ['a' => 1] ); + + $this->assertEquals( ['a' => 1, 2 => 'b'], $m->with( 2, 'b' )->toArray() ); + $this->assertEquals( ['a' => 1], $m->toArray() ); + $this->assertEquals( ['a' => 2], $m->with( 'a', 2 )->toArray() ); + $this->assertEquals( ['a' => 1], $m->toArray() ); + } + + public function testZip() { $m = new Map( [1, 2, 3] ); From 74a43d6c199ec2cfe11115d6d5ce17ddfa7df688 Mon Sep 17 00:00:00 2001 From: Aimeos Date: Tue, 7 Nov 2023 12:09:47 +0100 Subject: [PATCH 2/2] Added supported PHP versions and minor improvement for header --- README.md | 5 +++++ assets/css/style.scss | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 485196b..4e8fe5c 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,11 @@ as offered by jQuery and Laravel Collections. composer req aimeos/map ``` +Supported PHP versions: + +* PHP 7.1+ +* PHP 8+ + **Table of contents** * [Why PHP Map](#why-php-map) diff --git a/assets/css/style.scss b/assets/css/style.scss index 5a7c357..09b8782 100644 --- a/assets/css/style.scss +++ b/assets/css/style.scss @@ -19,6 +19,10 @@ header.page-header { background-image: linear-gradient(120deg, #16629c, #2091c5); } +header h1 { + overflow: hidden; +} + header h1>a { color: #fff; }