Skip to content

Pluraliser #196

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

Merged
merged 2 commits into from
Dec 28, 2020
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
31 changes: 31 additions & 0 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

class StringUtils
{
/**
* @var array<string,string>
*/
private static $pluraliseSearchReplace = [
'Property "%s" was' => 'Properties "%s" were',
'Property' => 'Properties',
];

public static function canonicalisePath(string $filename): string
{
$path = [];
Expand All @@ -30,4 +38,27 @@ public static function canonicalisePath(string $filename): string
? '/' . implode('/', $path)
: implode('/', $path);
}


/**
* @param string $string
* @param array<mixed> $items
* @param string ...$args
* @return string
*/
public static function pluralise(string $string, array $items, string ...$args): string
{
if (count($items) <= 1) {
return vsprintf($string, $args);
}

return vsprintf(
str_replace(
array_keys(static::$pluraliseSearchReplace),
array_values(static::$pluraliseSearchReplace),
$string
),
$args
);
}
}
14 changes: 14 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ function canonicalise_path(string $path): string
}
}

if (!function_exists('pluralise')) {

/**
* @param string $string
* @param array<mixed> $items
* @param string[] ...$args
* @return string
*/
function pluralise(string $string, array $items, string ...$args): string
{
return StringUtils::pluralise($string, $items, ...$args);
}
}

if (!function_exists('collect')) {

/**
Expand Down
39 changes: 39 additions & 0 deletions test/Util/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,43 @@ public function testExceptionIsThrownIfTryingToTraverseUpPastRoom(): void

StringUtils::canonicalisePath('/path/to/../../../');
}

/**
* @dataProvider pluraliseMultipleProvider
*/
public function testPluraliseWithMultipleValues(string $string, string $expected): void
{
$props = ['propOne', 'propTwo'];
$this->assertEquals($expected, StringUtils::pluralise($string, $props, implode('" & "', $props)));
}

public function pluraliseMultipleProvider(): array
{
return [
[
'Property "%s" should not have changed type',
'Properties "propOne" & "propTwo" should not have changed type'
],
['Property "%s" was not promoted', 'Properties "propOne" & "propTwo" were not promoted'],
['Property "%s" was missing', 'Properties "propOne" & "propTwo" were missing'],
];
}

/**
* @dataProvider pluraliseSingularProvider
*/
public function testPluraliseWithSingularValues(string $string, string $expected): void
{
$props = ['propOne'];
$this->assertEquals($expected, StringUtils::pluralise($string, $props, implode('" & "', $props)));
}

public function pluraliseSingularProvider(): array
{
return [
['Property "%s" should not have changed type', 'Property "propOne" should not have changed type'],
['Property "%s" was not promoted', 'Property "propOne" was not promoted'],
['Property "%s" was missing', 'Property "propOne" was missing'],
];
}
}