Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
46.15% |
6 / 13 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TranslationString | |
46.15% |
6 / 13 |
|
40.00% |
2 / 5 |
25.61 | |
0.00% |
0 / 1 |
| getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toUnbounded | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLastTranslationSegment | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| createForResourceName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\BoundedContext\BoundedContextId; |
| 6 | use Apie\Core\Identifiers\SnakeCaseSlug; |
| 7 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 8 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
| 9 | use ReflectionClass; |
| 10 | |
| 11 | #[Description('Represents a translation string/identifier')] |
| 12 | final class TranslationString implements HasRegexValueObjectInterface |
| 13 | { |
| 14 | use IsStringWithRegexValueObject; |
| 15 | |
| 16 | public static function getRegularExpression(): string |
| 17 | { |
| 18 | return '/^[a-z0-9_]+(\.[a-z0-9_]+)*$/i'; |
| 19 | } |
| 20 | |
| 21 | public function toPath(string $rootPath): string |
| 22 | { |
| 23 | return rtrim($rootPath, '/') . '/' . str_replace('.', '/', $this->internal); |
| 24 | } |
| 25 | |
| 26 | public function toUnbounded(): self |
| 27 | { |
| 28 | if (str_starts_with('apie.bounded.', $this->internal)) { |
| 29 | return new self(preg_replace('/^apie\.bounded\.[a-z0-9_]+\./i', 'apie.', $this->internal)); |
| 30 | } |
| 31 | return $this; |
| 32 | } |
| 33 | |
| 34 | public function getLastTranslationSegment(bool $trimUnderscoreAtStart = true): string |
| 35 | { |
| 36 | $fn = $trimUnderscoreAtStart ? function ($v) { return ltrim($v, '_'); } : function ($v) { return $v; }; |
| 37 | $pos = strrpos($this->internal, '.'); |
| 38 | if ($pos === false || $pos === 0) { |
| 39 | return $fn($this->internal); |
| 40 | } |
| 41 | return $fn(substr(strrchr($this->internal, '.'), 1)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param ReflectionClass<object> $class |
| 46 | */ |
| 47 | public static function createForResourceName(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): self |
| 48 | { |
| 49 | if ($boundedContextId === null) { |
| 50 | return new self('apie.resource.' . SnakeCaseSlug::fromClass($class) . '.singular'); |
| 51 | } |
| 52 | return new self('apie.bounded.' . $boundedContextId . '.resource.' . SnakeCaseSlug::fromClass($class) . '.singular'); |
| 53 | } |
| 54 | } |