Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
92 changes: 92 additions & 0 deletions src/MappedWithKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace ElegantBro\Arrayee;

use ElegantBro\Interfaces\Arrayee;
use Exception;
use function call_user_func;

/**
* @template In
* @template Out
*/
final class MappedWithKeys implements Arrayee
{
/**
* @var Arrayee
*/
private $arrayee;

/**
* @var callable
*/
private $valFunc;

/**
* @var callable
*/
private $keyFunc;

/**
* @param Arrayee<In> $arrayee
* @param callable $valFunc
* @return self<Out>
*/
public static function preserveKeys(Arrayee $arrayee, callable $valFunc): self
{
return new self(
$arrayee,
static function ($key) {
return $key;
},
$valFunc
);
}

/**
* @param Arrayee<In> $arrayee
* @param callable $keyFunc
* @return self<Out>
*/
public static function preserveValues(Arrayee $arrayee, callable $keyFunc): self
{
return new self(
$arrayee,
$keyFunc,
static function ($key, $val) {
return $val;
},
);
}

/**
* MappedWithKeys constructor.
* @param Arrayee<In> $arrayee
* @param callable $keyFunc
* @param callable $valFunc
*/
public function __construct(Arrayee $arrayee, callable $keyFunc, callable $valFunc)
{
$this->arrayee = $arrayee;
$this->keyFunc = $keyFunc;
$this->valFunc = $valFunc;
}

/**
* @return array<Out>
* @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;
}
}
89 changes: 89 additions & 0 deletions tests/MappedWithKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace ElegantBro\Arrayee\Tests;

use ElegantBro\Arrayee\Just;
use ElegantBro\Arrayee\MappedWithKeys;
use Exception;
use PHPUnit\Framework\TestCase;
use function strtoupper;

final class MappedWithKeysTest extends TestCase
{
/**
* @throws Exception
*/
public function testAsArray(): void
{
self::assertEquals(
[
2 => '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()
);
}
}