From a814a99876a8c2a48bba3c757901e93131877622 Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Thu, 20 Jun 2024 08:53:56 +0200 Subject: [PATCH 1/3] Add `ArrayUtils::hasDuplicateValue` + tests --- CHANGELOG.md | 4 ++++ src/Utils/ArrayUtils.php | 8 ++++++++ tests/Utils/ArrayUtilsTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f02dd4..c22f6b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ CHANGELOG for 1.x =================== +## v1.7.1 - (2024-06-20) +### Added +- `ArrayUtils::hasDuplicateValue` + tests + ## v1.7.0 - (2024-06-14) ### Added - `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring diff --git a/src/Utils/ArrayUtils.php b/src/Utils/ArrayUtils.php index fabfec1..ded0273 100644 --- a/src/Utils/ArrayUtils.php +++ b/src/Utils/ArrayUtils.php @@ -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)); + } } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index 00ea48b..e5e9d3b 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -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] + ], + ]; + } } From 790f466a99904c9cc7f2c8bb743599639d103fed Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Thu, 20 Jun 2024 09:00:09 +0200 Subject: [PATCH 2/3] Add `StringUtils::fillPrefix` Fill a prefix to value until specified length + tests --- CHANGELOG.md | 1 + src/Utils/StringUtils.php | 18 ++++++++++++++++++ tests/Utils/StringUtilsTest.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c22f6b5..6cc7866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG for 1.x ## v1.7.1 - (2024-06-20) ### Added - `ArrayUtils::hasDuplicateValue` + tests +- `StringUtils::fillPrefix` Fill a prefix to value until specified length + tests ## v1.7.0 - (2024-06-14) ### Added diff --git a/src/Utils/StringUtils.php b/src/Utils/StringUtils.php index 678e34e..9697f94 100644 --- a/src/Utils/StringUtils.php +++ b/src/Utils/StringUtils.php @@ -186,4 +186,22 @@ public static function formatSpaceBetween(?string $first, ?string $last): string return $first . $space . $last; } + + /** + * Fill a prefix to value until specified length + * + *
+     * 
+     * 
+ * The above example will output: + *
+     * '0001'
+     * 
+ */ + public static function fillPrefix(int|string $value, int $length, string $prefixValue): string + { + return sprintf("%$prefixValue{$length}s", $value); + } } diff --git a/tests/Utils/StringUtilsTest.php b/tests/Utils/StringUtilsTest.php index be73fe3..674d68a 100644 --- a/tests/Utils/StringUtilsTest.php +++ b/tests/Utils/StringUtilsTest.php @@ -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' + ], + ]; + } } From f816ad672c6ad9e72e5f2ecf7c83e63930d16016 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Fri, 2 Aug 2024 09:43:08 +0200 Subject: [PATCH 3/3] Move temporary changelog + add comment example of where to use fillPrefix --- CHANGELOG.md | 5 ----- CHANGELOG_add_duplicate_value_fill_prefix.md | 3 +++ src/Utils/StringUtils.php | 3 ++- 3 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 CHANGELOG_add_duplicate_value_fill_prefix.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cc7866..2f02dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,5 @@ CHANGELOG for 1.x =================== -## v1.7.1 - (2024-06-20) -### Added -- `ArrayUtils::hasDuplicateValue` + tests -- `StringUtils::fillPrefix` Fill a prefix to value until specified length + tests - ## v1.7.0 - (2024-06-14) ### Added - `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring diff --git a/CHANGELOG_add_duplicate_value_fill_prefix.md b/CHANGELOG_add_duplicate_value_fill_prefix.md new file mode 100644 index 0000000..7387d52 --- /dev/null +++ b/CHANGELOG_add_duplicate_value_fill_prefix.md @@ -0,0 +1,3 @@ +### Added +- `MonitoringApiControllerTrait` used to centralize ApiCall manipulation during api monitoring +- `ApiCallMonitor::logException` simplify logging exception for `ApiCall` using the `ProcessMonitor` diff --git a/src/Utils/StringUtils.php b/src/Utils/StringUtils.php index 9697f94..4ab3c7c 100644 --- a/src/Utils/StringUtils.php +++ b/src/Utils/StringUtils.php @@ -188,7 +188,8 @@ public static function formatSpaceBetween(?string $first, ?string $last): string } /** - * Fill a prefix to value until specified length + * Fill a prefix to value until specified length. + * For example this can be used on invoice number generation to fill in missing character. * *
      *