Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.59% |
25 / 27 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MenuHeader | |
92.59% |
25 / 27 |
|
50.00% |
2 / 4 |
10.04 | |
0.00% |
0 / 1 |
| getFallbackText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| asId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createChildNode | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| createRoot | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
4.00 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\ExampleValue; |
| 6 | use Apie\Core\Context\ApieContext; |
| 7 | use Apie\Core\Identifiers\SnakeCaseSlug; |
| 8 | use Apie\Core\Lists\ItemHashmap; |
| 9 | |
| 10 | #[Description('Menu header translations. Keys reflect the tree structure of the menu')] |
| 11 | #[ExampleValue('apie.menu.header.root.test.authenticated')] |
| 12 | final class MenuHeader extends AbstractTranslation |
| 13 | { |
| 14 | protected const MIDDLE_REGEX = '(menu.header|(menu(\.[^.]+(\.[^.]+)*))*\.header)'; |
| 15 | |
| 16 | public function getFallbackText(): string |
| 17 | { |
| 18 | if (preg_match('/\.([^.]+)\.header$/', $this->middleSection, $matches)) { |
| 19 | return $matches[1] ? ucfirst(SnakeCaseSlug::fromText($matches[1])->humanize()) : 'Home'; |
| 20 | } |
| 21 | |
| 22 | return 'Home'; |
| 23 | } |
| 24 | |
| 25 | public function asId(): string |
| 26 | { |
| 27 | return str_replace('.', '_', $this->middleSection); |
| 28 | } |
| 29 | |
| 30 | public function createChildNode(string $part): MenuHeader |
| 31 | { |
| 32 | if ($part) { |
| 33 | $part = SnakeCaseSlug::fromText($part)->toNative(); |
| 34 | $middle = preg_replace('/\.header$/', '.' . $part . '.header', $this->middleSection); |
| 35 | return new static($this->prefix, $middle, $this->suffix, $this->placeholderValue); |
| 36 | } |
| 37 | |
| 38 | return $this; |
| 39 | } |
| 40 | |
| 41 | public static function createRoot(ApieContext $context = new ApieContext(), string $path = ''): MenuHeader |
| 42 | { |
| 43 | $prefix = TranslationStringPrefix::fromApieContext($context); |
| 44 | $suffix = TranslationStringSuffix::fromApieContext($context); |
| 45 | $pathList = array_filter( |
| 46 | array_map( |
| 47 | function (string $pathItem) { |
| 48 | if ($pathItem === 'header') { |
| 49 | return 'header2'; |
| 50 | } |
| 51 | return $pathItem ? SnakeCaseSlug::fromText($pathItem)->toNative() : ''; |
| 52 | }, |
| 53 | explode('.', $path) |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | return new static( |
| 58 | $prefix, |
| 59 | $path ? ('menu.' . implode('.', $pathList) . '.header') : 'menu.header', |
| 60 | $suffix, |
| 61 | new ItemHashmap() |
| 62 | ); |
| 63 | } |
| 64 | } |