Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.30% |
26 / 27 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ResourceName | |
96.30% |
26 / 27 |
|
66.67% |
2 / 3 |
8 | |
0.00% |
0 / 1 |
| getFallbackText | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
| createFromTuple | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| getSimplifications | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Actions\BoundedContextEntityTuple; |
| 5 | use Apie\Core\Attributes\Description; |
| 6 | use Apie\Core\Attributes\ExampleValue; |
| 7 | use Apie\Core\Identifiers\SnakeCaseSlug; |
| 8 | use Apie\Core\Lists\ItemHashmap; |
| 9 | use Apie\Core\Translator\Enums\Pluralization; |
| 10 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 11 | use ICanBoogie\Inflector; |
| 12 | |
| 13 | #[Description('Name of resource')] |
| 14 | #[ExampleValue('apie.bounded.test.resource.user.name.singular')] |
| 15 | class ResourceName extends AbstractTranslation |
| 16 | { |
| 17 | protected const MIDDLE_REGEX = 'name'; |
| 18 | |
| 19 | public function getFallbackText(string $locale = 'en'): string |
| 20 | { |
| 21 | $id = $this->prefix->getResourceIdentifier(); |
| 22 | $plural = $this->suffix->getPluralization(); |
| 23 | if ($id) { |
| 24 | $inflector = Inflector::get($locale); |
| 25 | return match ($plural) { |
| 26 | Pluralization::Singular => $inflector->singularize(ucfirst($id->humanize())), |
| 27 | Pluralization::Plural => $inflector->pluralize(ucfirst($id->humanize())), |
| 28 | default => ucfirst($id->humanize()), |
| 29 | }; |
| 30 | } |
| 31 | return 'Resource'; |
| 32 | } |
| 33 | |
| 34 | public static function createFromTuple(BoundedContextEntityTuple $tuple, TranslationStringSuffix $suffix = new TranslationStringSuffix()): static |
| 35 | { |
| 36 | return new static( |
| 37 | new TranslationStringPrefix( |
| 38 | $tuple->boundedContext->getId(), |
| 39 | SnakeCaseSlug::fromClass($tuple->resourceClass) |
| 40 | ), |
| 41 | 'name', |
| 42 | $suffix, |
| 43 | new ItemHashmap() |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function getSimplifications(): TranslationStringSet |
| 48 | { |
| 49 | $list = []; |
| 50 | $prefixSimplification = $this->prefix->withoutBoundedContextId(); |
| 51 | foreach ($this->suffix->getSimplifications() as $suffixSimplification) { |
| 52 | $list[] = new static( |
| 53 | $prefixSimplification, |
| 54 | $this->middleSection, |
| 55 | $suffixSimplification, |
| 56 | $this->placeholderValue |
| 57 | ); |
| 58 | } |
| 59 | return new TranslationStringSet($list); |
| 60 | } |
| 61 | } |