-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! fixup! fixup! fixup! fixup! fixup! Merge pull request #29 from…
… TomasVotruba/training
- Loading branch information
1 parent
1775aeb
commit b22a581
Showing
5 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
projects/open-training/src/Exception/Twig/InvalidWordCountException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OpenTraining\Exception\Twig; | ||
|
||
use Exception; | ||
|
||
final class InvalidWordCountException extends Exception | ||
{ | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
projects/open-training/src/Twig/Extension/InflectionExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OpenTraining\Twig\Extension; | ||
|
||
use OpenTraining\Exception\Twig\InvalidWordCountException; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* @docs https://symfony.com/doc/current/templating/twig_extension.html | ||
* | ||
* Plural, singular, | ||
*/ | ||
final class InflectionExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @return TwigFunction[] | ||
* | ||
* @use "<a href="..." class="{{ active_item('blog']) }}">" | ||
* @use "<a href="..." class="{{ active_item(['trainings', 'training-detail']) }}">" | ||
*/ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('word_by_count', function (int $count, array $versions): string { | ||
$this->ensureValidFormCountIsProvided($versions); | ||
if ($count === 1) { | ||
return $count . ' ' . $versions[0]; | ||
} | ||
|
||
if ($count === 0 || $count < 5) { | ||
return $count . ' ' . $versions[1]; | ||
} | ||
|
||
return $count . ' ' . $versions[2]; | ||
}), | ||
]; | ||
} | ||
|
||
/** | ||
* @param string[] $versions | ||
*/ | ||
private function ensureValidFormCountIsProvided(array $versions): void | ||
{ | ||
if (count($versions) === 3) { | ||
return; | ||
} | ||
|
||
throw new InvalidWordCountException(sprintf( | ||
'Provide exactly 3 options to word_by_count() function as 2nd argument. %d given', | ||
count($versions) | ||
)); | ||
} | ||
} |