Skip to content

Commit

Permalink
fix: multiple check column/keys from array
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Jan 22, 2024
1 parent 52a0673 commit a81e61d
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 93 deletions.
1 change: 1 addition & 0 deletions app/Controllers/Common.php
1 change: 1 addition & 0 deletions app/Controllers/Language
35 changes: 17 additions & 18 deletions system/Helpers/Array/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,37 +321,36 @@ public static function sortValuesByNatural(array &$array, $sortByIndex = null):
}

/**
* Duplicate Array Check by Column
* Returns duplicate elements from an array by key(s).
*
* @param array|string $column Unique Column
* @param array $data Array Data
* @param array|string $key Key(s) to check
* @param array $array Array Data
*/
public static function arrayDuplicatesBy($column, array $data = []): array
public static function duplicatesBy($key, array $array = []): array
{
if ($data === []) {
if ($array === []) {
return [];
}

$duplicateData = [];
$dataUnique = [];

foreach ($data as $lineIndex => $searchData) {
if (is_array($column)) {
foreach ($column as $rawColumn) {
if (in_array($searchData[$rawColumn], $dataUnique[$rawColumn] ?? [], true)) {
foreach ($array as $lineIndex => $searchData) {
// If the specified column is an array, generate a unique key using implode
// to concatenate values of specified columns and create a unique identifier
$keyColumns = is_array($key) ? implode('-', array_intersect_key($searchData, array_flip($key))) : $searchData[$key];

// Check for duplicates based on the generated key
if (isset($dataUnique[$keyColumns])) {
if (is_array($key)) {
foreach ($key as $rawColumn) {
$duplicateData[$lineIndex][$rawColumn] = $searchData[$rawColumn];
} else {
$dataUnique[$rawColumn][] = $searchData[$rawColumn];
}
}
}

if (is_string($column)) {
if (in_array($searchData[$column], $dataUnique[$column] ?? [], true)) {
$duplicateData[$lineIndex][$column] = $searchData[$column];
} else {
$dataUnique[$column][] = $searchData[$column];
$duplicateData[$lineIndex][$key] = $searchData[$key];
}
} else {
$dataUnique[$keyColumns] = true;
}
}

Expand Down
10 changes: 5 additions & 5 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

if (! function_exists('array_duplicate_by')) {
/**
* Duplicate array check by column.
* Returns duplicate elements from an array by key(s).
*
* @param array|string $column Unique Column
* @param array $data Array Data
* @param array|string $key Key(s) to check
* @param array $array Array Data
*
* @return array
*/
function array_duplicate_by($column, array $data = [])
function array_duplicate_by($key, array $array = [])
{
return ArrayHelper::arrayDuplicatesBy($column, $data);
return ArrayHelper::duplicatesBy($key, $array);
}
}

Expand Down
54 changes: 37 additions & 17 deletions tests/system/Helpers/Array/ArrayHelperDuplicateCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,65 @@ final class ArrayHelperDuplicateCheckTest extends CIUnitTestCase
{
private array $array = [
[
'name' => 'Fred Flinstone',
'age' => 20,
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'name' => 'Brad Pierce',
'age' => 30,
'provider_name' => 'Rumah Sakit Silorm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.72',
'provider_address' => 'wwaasawdasa',
],
[
'name' => 'Fred Flinstone',
'age' => 70,
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'name' => 'Michelle Stone',
'age' => 30,
'provider_name' => 'Rumah Sakit Siloum',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.74',
'provider_address' => 'wwaasawdasa',
],
[
'name' => 'Michael Bram',
'age' => 40,
'provider_name' => 'Rumah Sakit Siloem',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.75',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Silosm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.76',
'provider_address' => 'wwaasawdasa',
],
];

public function testSingleColumn(): void
{
$this->assertSame([
2 => [
'name' => 'Fred Flinstone',
'provider_name' => 'Rumah Sakit Siloam',
],
], ArrayHelper::arrayDuplicatesBy('name', $this->array));
], ArrayHelper::duplicatesBy('provider_name', $this->array));
}

public function testMultipleColumn(): void
{
$this->assertSame([
2 => [
'name' => 'Fred Flinstone',
],
3 => [
'age' => 30,
'provider_name' => 'Rumah Sakit Siloam',
'provider_region' => '31.71',
],
], ArrayHelper::arrayDuplicatesBy(['name', 'age'], $this->array));
], ArrayHelper::duplicatesBy(['provider_name', 'provider_region'], $this->array));
}
}
152 changes: 99 additions & 53 deletions tests/system/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,73 +1291,119 @@ public static function provideArrayGroupByExcludeEmpty(): iterable
/**
* @dataProvider provideArrayDuplicate
*
* @param mixed $column
* @param array|string $key Key(s) to Check
*/
public function testArrayDuplicate($column, array $data, array $expected): void
public function testArrayDuplicate($key, array $data, array $expected): void
{
$this->assertSame($expected, array_duplicate_by($column, $data));
$this->assertSame($expected, array_duplicate_by($key, $data));
}

public static function provideArrayDuplicate(): iterable
{
yield 'single and multiple' => [
'name', // Single
[
[
'name' => 'Fred Flinstone',
'age' => 20,
],
[
'name' => 'Brad Pierce',
'age' => 30,
],
[
'name' => 'Fred Flinstone',
'age' => 70,
],
yield from [
'single' => [
'provider_name', // Single
[
'name' => 'Michelle Stone',
'age' => 30,
[
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Silorm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.72',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloum',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.74',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloem',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.75',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Silosm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.76',
'provider_address' => 'wwaasawdasa',
],
],
[
'name' => 'Michael Bram',
'age' => 40,
],
],
[
2 => [
'name' => 'Fred Flinstone',
2 => [
'provider_name' => 'Rumah Sakit Siloam',
],
],
],
['name', 'age'], // Multiple
[
'multiple' => [
['provider_name', 'provider_region'], // Multiple
[
'name' => 'Fred Flinstone',
'age' => 20,
],
[
'name' => 'Brad Pierce',
'age' => 30,
],
[
'name' => 'Fred Flinstone',
'age' => 70,
],
[
'name' => 'Michelle Stone',
'age' => 30,
[
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Silorm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.72',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloam',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.71',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloum',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.74',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Siloem',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.75',
'provider_address' => 'wwaasawdasa',
],
[
'provider_name' => 'Rumah Sakit Silosm',
'provider_email' => '[email protected]',
'provider_website' => 'example.com',
'provider_region' => '31.76',
'provider_address' => 'wwaasawdasa',
],
],
[
'name' => 'Michael Bram',
'age' => 40,
],
],
[
2 => [
'name' => 'Fred Flinstone',
],
3 => [
'age' => 30,
2 => [
'provider_name' => 'Rumah Sakit Siloam',
'provider_region' => '31.71',
],
],
],
];
Expand Down

0 comments on commit a81e61d

Please sign in to comment.