-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move initialization logic out from the builder, extract array util he…
…lper class
- Loading branch information
Showing
5 changed files
with
122 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Util; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ArrayUtil | ||
{ | ||
/** | ||
* Maps an array with callable that returns an array of key-value pairs. | ||
* | ||
* <code> | ||
* $map = ArrayUtil::mapWithKeys( | ||
* callback: fn (array $array) => [$array['id'] => $array['name']], | ||
* array: [ | ||
* ['id' => 1, 'name' => 'John'], | ||
* ['id' => 2, 'name' => 'Jane'], | ||
* ] | ||
* ); | ||
* | ||
* var_dump($map); // array(2) { [1] => string(4) "John" [2] => string(4) "Jane" } | ||
* </code> | ||
* | ||
* @param callable(mixed): array<int|string, mixed> $callback | ||
*/ | ||
public static function mapWithKeys(callable $callback, array $array): array | ||
{ | ||
$data = []; | ||
|
||
foreach ($array as $value) { | ||
foreach ($callback($value) as $mapKey => $mapValue) { | ||
$data[$mapKey] = $mapValue; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Performs a `ksort` on a copy of given array instead of modifying it by reference. | ||
*/ | ||
public static function ksort(array $array, int $flags = SORT_REGULAR): array | ||
{ | ||
$copy = $array; | ||
|
||
ksort($copy, $flags); | ||
|
||
return $copy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Util; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Util\ArrayUtil; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArrayUtilTest extends TestCase | ||
{ | ||
public function testKsort() | ||
{ | ||
$input = ['c' => 3, 'b' => 2, 'a' => 1]; | ||
$output = ArrayUtil::ksort($input); | ||
|
||
$this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], $output); | ||
$this->assertSame(['c' => 3, 'b' => 2, 'a' => 1], $input); | ||
} | ||
|
||
public function testMapWithKeys() | ||
{ | ||
$input = [ | ||
['id' => 1, 'name' => 'John'], | ||
['id' => 2, 'name' => 'Jane'], | ||
]; | ||
|
||
$output = ArrayUtil::mapWithKeys( | ||
callback: fn (array $array) => [$array['id'] => $array['name']], | ||
array: $input, | ||
); | ||
|
||
$this->assertSame([1 => 'John', 2 => 'Jane'], $output); | ||
$this->assertSame([ | ||
['id' => 1, 'name' => 'John'], | ||
['id' => 2, 'name' => 'Jane'], | ||
], $input); | ||
} | ||
} |