Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArrayUtils::hasDuplicateValue and StringUtils::fillPrefix #35

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
],
];
}
}