Skip to content

Commit 24309da

Browse files
authored
Merge pull request #196 from php-school/str-pluraliser
Pluraliser
2 parents dc62895 + fd8cd1b commit 24309da

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/Utils/StringUtils.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
class StringUtils
1010
{
11+
/**
12+
* @var array<string,string>
13+
*/
14+
private static $pluraliseSearchReplace = [
15+
'Property "%s" was' => 'Properties "%s" were',
16+
'Property' => 'Properties',
17+
];
18+
1119
public static function canonicalisePath(string $filename): string
1220
{
1321
$path = [];
@@ -30,4 +38,27 @@ public static function canonicalisePath(string $filename): string
3038
? '/' . implode('/', $path)
3139
: implode('/', $path);
3240
}
41+
42+
43+
/**
44+
* @param string $string
45+
* @param array<mixed> $items
46+
* @param string ...$args
47+
* @return string
48+
*/
49+
public static function pluralise(string $string, array $items, string ...$args): string
50+
{
51+
if (count($items) <= 1) {
52+
return vsprintf($string, $args);
53+
}
54+
55+
return vsprintf(
56+
str_replace(
57+
array_keys(static::$pluraliseSearchReplace),
58+
array_values(static::$pluraliseSearchReplace),
59+
$string
60+
),
61+
$args
62+
);
63+
}
3364
}

src/functions.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ function canonicalise_path(string $path): string
4747
}
4848
}
4949

50+
if (!function_exists('pluralise')) {
51+
52+
/**
53+
* @param string $string
54+
* @param array<mixed> $items
55+
* @param string[] ...$args
56+
* @return string
57+
*/
58+
function pluralise(string $string, array $items, string ...$args): string
59+
{
60+
return StringUtils::pluralise($string, $items, ...$args);
61+
}
62+
}
63+
5064
if (!function_exists('collect')) {
5165

5266
/**

test/Util/StringUtilsTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,43 @@ public function testExceptionIsThrownIfTryingToTraverseUpPastRoom(): void
2929

3030
StringUtils::canonicalisePath('/path/to/../../../');
3131
}
32+
33+
/**
34+
* @dataProvider pluraliseMultipleProvider
35+
*/
36+
public function testPluraliseWithMultipleValues(string $string, string $expected): void
37+
{
38+
$props = ['propOne', 'propTwo'];
39+
$this->assertEquals($expected, StringUtils::pluralise($string, $props, implode('" & "', $props)));
40+
}
41+
42+
public function pluraliseMultipleProvider(): array
43+
{
44+
return [
45+
[
46+
'Property "%s" should not have changed type',
47+
'Properties "propOne" & "propTwo" should not have changed type'
48+
],
49+
['Property "%s" was not promoted', 'Properties "propOne" & "propTwo" were not promoted'],
50+
['Property "%s" was missing', 'Properties "propOne" & "propTwo" were missing'],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider pluraliseSingularProvider
56+
*/
57+
public function testPluraliseWithSingularValues(string $string, string $expected): void
58+
{
59+
$props = ['propOne'];
60+
$this->assertEquals($expected, StringUtils::pluralise($string, $props, implode('" & "', $props)));
61+
}
62+
63+
public function pluraliseSingularProvider(): array
64+
{
65+
return [
66+
['Property "%s" should not have changed type', 'Property "propOne" should not have changed type'],
67+
['Property "%s" was not promoted', 'Property "propOne" was not promoted'],
68+
['Property "%s" was missing', 'Property "propOne" was missing'],
69+
];
70+
}
3271
}

0 commit comments

Comments
 (0)