From c683609a73c862be16e328e554a4fe176aac4515 Mon Sep 17 00:00:00 2001 From: Pavel Stepanets Date: Fri, 16 Apr 2021 11:57:19 +0700 Subject: [PATCH 1/2] MappedWithKeys introduced --- src/MappedWithKeys.php | 75 ++++++++++++++++++++++++++++++ tests/MappedWithKeysTest.php | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/MappedWithKeys.php create mode 100644 tests/MappedWithKeysTest.php diff --git a/src/MappedWithKeys.php b/src/MappedWithKeys.php new file mode 100644 index 0000000..ca9e993 --- /dev/null +++ b/src/MappedWithKeys.php @@ -0,0 +1,75 @@ +arrayee = $arrayee; + $this->keyFunc = $keyFunc; + $this->valFunc = $valFunc; + } + + /** + * @return array + * @throws Exception + */ + public function asArray(): array + { + $res = []; + foreach ($this->arrayee->asArray() as $key => $val) { + $res[ + call_user_func($this->keyFunc, $key, $val) + ] = call_user_func($this->valFunc, $key, $val); + } + + return $res; + } +} diff --git a/tests/MappedWithKeysTest.php b/tests/MappedWithKeysTest.php new file mode 100644 index 0000000..bb622c3 --- /dev/null +++ b/tests/MappedWithKeysTest.php @@ -0,0 +1,89 @@ + 'FOO', + 6 => 'BAR', + 10 => 'BAZ', + ], + (new MappedWithKeys( + new Just([ + 1 => 'foo', + 3 => 'bar', + 5 => 'baz', + ]), + static function(int $key): int { + return $key * 2; + }, + static function(int $key, string $val): string { + return strtoupper($val); + }, + ))->asArray() + ); + } + + /** + * @throws Exception + */ + public function testPreserveKeys(): void + { + self::assertEquals( + [ + 1 => 'FOO', + 3 => 'BAR', + 5 => 'BAZ', + ], + MappedWithKeys::preserveKeys( + new Just([ + 1 => 'foo', + 3 => 'bar', + 5 => 'baz', + ]), + static function(int $key, string $val): string { + return strtoupper($val); + }, + )->asArray() + ); + } + + /** + * @throws Exception + */ + public function testPreserveValues(): void + { + self::assertEquals( + [ + 2 => 'foo', + 6 => 'bar', + 10 => 'baz', + ], + MappedWithKeys::preserveValues( + new Just([ + 1 => 'foo', + 3 => 'bar', + 5 => 'baz', + ]), + static function(int $key): int { + return $key * 2; + }, + )->asArray() + ); + } +} From dd259682f732ec8952f11577bfc7c1c1de9c4fe0 Mon Sep 17 00:00:00 2001 From: Pavel Stepanets Date: Fri, 16 Apr 2021 12:17:40 +0700 Subject: [PATCH 2/2] loop with so-called generic --- Makefile | 3 +++ src/MappedWithKeys.php | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b78ec91..3eb5475 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ style-check: style-fix: $(docker) vendor/bin/phpcbf -p --standard=PSR12 src +stan: + $(docker) vendor/bin/phpstan analyse + unit: $(docker) -dzend_extension=xdebug.so -dxdebug.mode=coverage vendor/bin/phpunit diff --git a/src/MappedWithKeys.php b/src/MappedWithKeys.php index ca9e993..41f884c 100644 --- a/src/MappedWithKeys.php +++ b/src/MappedWithKeys.php @@ -9,7 +9,8 @@ use function call_user_func; /** - * @template T + * @template In + * @template Out */ final class MappedWithKeys implements Arrayee { @@ -28,6 +29,11 @@ final class MappedWithKeys implements Arrayee */ private $keyFunc; + /** + * @param Arrayee $arrayee + * @param callable $valFunc + * @return self + */ public static function preserveKeys(Arrayee $arrayee, callable $valFunc): self { return new self( @@ -39,6 +45,11 @@ static function ($key) { ); } + /** + * @param Arrayee $arrayee + * @param callable $keyFunc + * @return self + */ public static function preserveValues(Arrayee $arrayee, callable $keyFunc): self { return new self( @@ -50,6 +61,12 @@ static function ($key, $val) { ); } + /** + * MappedWithKeys constructor. + * @param Arrayee $arrayee + * @param callable $keyFunc + * @param callable $valFunc + */ public function __construct(Arrayee $arrayee, callable $keyFunc, callable $valFunc) { $this->arrayee = $arrayee; @@ -58,7 +75,7 @@ public function __construct(Arrayee $arrayee, callable $keyFunc, callable $valFu } /** - * @return array + * @return array * @throws Exception */ public function asArray(): array