Skip to content

Commit

Permalink
Merge pull request #35 from smartbooster/add_duplicate_value_fill_prefix
Browse files Browse the repository at this point in the history
Add `ArrayUtils::hasDuplicateValue` and `StringUtils::fillPrefix`
  • Loading branch information
mathieu-ducrot authored Aug 2, 2024
2 parents 2b94b04 + f816ad6 commit 193ccbf
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_add_duplicate_value_fill_prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added
- `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring
- `ApiCallMonitor::logException` simplify logging exception for `ApiCall` using the `ProcessMonitor`
8 changes: 8 additions & 0 deletions src/Utils/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,12 @@ public static function flatToMap(?array $array, ?\Closure $fnKey, ?\Closure $fnV

return array_combine($keys, $values);
}

/**
* Inspiration : https://stackoverflow.com/questions/3145607/php-check-if-an-array-has-duplicates
*/
public static function hasDuplicateValue(array $array): bool
{
return count($array) !== count(array_flip($array));
}
}
19 changes: 19 additions & 0 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,23 @@ public static function formatSpaceBetween(?string $first, ?string $last): string

return $first . $space . $last;
}

/**
* Fill a prefix to value until specified length.
* For example this can be used on invoice number generation to fill in missing character.
*
* <pre>
* <?php
* fillPrefix(1, 4, '0');
* ?>
* </pre>
* The above example will output:
* <pre>
* '0001'
* </pre>
*/
public static function fillPrefix(int|string $value, int $length, string $prefixValue): string
{
return sprintf("%$prefixValue{$length}s", $value);
}
}
30 changes: 30 additions & 0 deletions tests/Utils/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,34 @@ function (int $value) {
],
];
}

/**
* @dataProvider hasDuplicateProvider
*/
public function testHasDuplicateValue(bool $expected, array $array): void
{
$this->assertSame($expected, ArrayUtils::hasDuplicateValue($array));
}

public function hasDuplicateProvider(): array
{
return [
'array_with_string_duplicated' => [
true,
['a', 'b', 'c', 'a']
],
'array_with_string_not_duplicated' => [
false,
['a', 'b', 'c']
],
'array_with_int_duplicated' => [
true,
[1, 15, 15, 50]
],
'array_with_int_not_duplicated' => [
false,
[18, 10, 88]
],
];
}
}
32 changes: 32 additions & 0 deletions tests/Utils/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,36 @@ public function formatSpaceBetweenProvider(): array
]
];
}

/**
* @dataProvider fillPrefixProvider
*/
public function testFillPrefix(string $expected, int|string $value, int $length, string $prefixValue): void
{
$this->assertSame($expected, StringUtils::fillPrefix($value, $length, $prefixValue));
}

public function fillPrefixProvider(): array
{
return [
'simple' => [
'0001',
1,
4,
'0'
],
'full' => [
'99999',
'99999',
5,
'0'
],
'big_prefix' => [
'0011',
11,
4,
'000000000000000'
],
];
}
}

0 comments on commit 193ccbf

Please sign in to comment.