Skip to content

Commit

Permalink
feat: get duplicate by column helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevsr committed Jan 22, 2024
1 parent cdaee9d commit 52a0673
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
40 changes: 40 additions & 0 deletions system/Helpers/Array/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,44 @@ public static function sortValuesByNatural(array &$array, $sortByIndex = null):
return strnatcmp((string) $currentValue, (string) $nextValue);
});
}

/**
* Duplicate Array Check by Column
*
* @param array|string $column Unique Column
* @param array $data Array Data
*/
public static function arrayDuplicatesBy($column, array $data = []): array
{
if ($data === []) {
return [];
}

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

foreach ($data as $lineIndex => $searchData) {
if (is_array($column)) {
foreach ($column as $rawColumn) {
if (in_array($searchData[$rawColumn], $dataUnique[$rawColumn] ?? [], true)) {
$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];
}
}
}

unset($dataUnique);

return $duplicateData;
}
}
15 changes: 15 additions & 0 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@

// CodeIgniter Array Helpers

if (! function_exists('array_duplicate_by')) {
/**
* Duplicate array check by column.
*
* @param array|string $column Unique Column
* @param array $data Array Data
*
* @return array
*/
function array_duplicate_by($column, array $data = [])
{
return ArrayHelper::arrayDuplicatesBy($column, $data);
}
}

if (! function_exists('dot_array_search')) {
/**
* Searches an array through dot syntax. Supports
Expand Down
68 changes: 68 additions & 0 deletions tests/system/Helpers/Array/ArrayHelperDuplicateCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Helpers\Array;

use CodeIgniter\Test\CIUnitTestCase;

/**
* @group Others
*
* @internal
*/
final class ArrayHelperDuplicateCheckTest extends CIUnitTestCase
{
private array $array = [
[
'name' => 'Fred Flinstone',
'age' => 20,
],
[
'name' => 'Brad Pierce',
'age' => 30,
],
[
'name' => 'Fred Flinstone',
'age' => 70,
],
[
'name' => 'Michelle Stone',
'age' => 30,
],
[
'name' => 'Michael Bram',
'age' => 40,
],
];

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

public function testMultipleColumn(): void
{
$this->assertSame([
2 => [
'name' => 'Fred Flinstone',
],
3 => [
'age' => 30,
],
], ArrayHelper::arrayDuplicatesBy(['name', 'age'], $this->array));
}
}
75 changes: 75 additions & 0 deletions tests/system/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1287,4 +1287,79 @@ public static function provideArrayGroupByExcludeEmpty(): iterable
],
];
}

/**
* @dataProvider provideArrayDuplicate
*
* @param mixed $column
*/
public function testArrayDuplicate($column, array $data, array $expected): void
{
$this->assertSame($expected, array_duplicate_by($column, $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,
],
[
'name' => 'Michelle Stone',
'age' => 30,
],
[
'name' => 'Michael Bram',
'age' => 40,
],
],
[
2 => [
'name' => 'Fred Flinstone',
],
],
['name', 'age'], // Multiple
[
[
'name' => 'Fred Flinstone',
'age' => 20,
],
[
'name' => 'Brad Pierce',
'age' => 30,
],
[
'name' => 'Fred Flinstone',
'age' => 70,
],
[
'name' => 'Michelle Stone',
'age' => 30,
],
[
'name' => 'Michael Bram',
'age' => 40,
],
],
[
2 => [
'name' => 'Fred Flinstone',
],
3 => [
'age' => 30,
],
],
];
}
}

0 comments on commit 52a0673

Please sign in to comment.