Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
60.00% covered (warning)
60.00%
12 / 20
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationString
60.00% covered (warning)
60.00%
12 / 20
62.50% covered (warning)
62.50%
5 / 8
23.82
0.00% covered (danger)
0.00%
0 / 1
 getRegularExpression
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSpecifity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toUnbounded
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getLastTranslationSegment
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
4.18
 getFallbackText
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSimplifications
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createForResourceName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\Core\Translator\ValueObjects;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\BoundedContext\BoundedContextId;
6use Apie\Core\Identifiers\SnakeCaseSlug;
7use Apie\Core\Translator\Lists\TranslationStringSet;
8use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
9use Apie\Core\ValueObjects\IsStringWithRegexValueObject;
10use ReflectionClass;
11
12#[Description('Represents a translation string/identifier')]
13final class TranslationString implements HasRegexValueObjectInterface
14{
15    use IsStringWithRegexValueObject;
16
17    public static function getRegularExpression(): string
18    {
19        return '/^[a-z0-9_]+(\.[a-z0-9_]+)*$/i';
20    }
21
22    public function toPath(string $rootPath): string
23    {
24        return rtrim($rootPath, '/') . '/' . str_replace('.', '/', $this->internal);
25    }
26
27    /**
28     * @internal
29     */
30    public function getSpecifity(): int
31    {
32        return 0;
33    }
34
35    public function toUnbounded(): self
36    {
37        if (str_starts_with('apie.bounded.', $this->internal)) {
38            return new self(preg_replace('/^apie\.bounded\.[a-z0-9_]+\./i', 'apie.', $this->internal));
39        }
40        return $this;
41    }
42
43    public function getLastTranslationSegment(bool $trimUnderscoreAtStart = true): string
44    {
45        $fn = $trimUnderscoreAtStart ? function ($v) {
46            return ltrim($v, '_');
47        } : function ($v) {
48            return $v;
49        };
50        $pos = strrpos($this->internal, '.');
51        if ($pos === false || $pos === 0) {
52            return $fn($this->internal);
53        }
54        return $fn(substr(strrchr($this->internal, '.'), 1));
55    }
56
57    public function getFallbackText(): string
58    {
59        return $this->getLastTranslationSegment();
60    }
61
62    public function getSimplifications(): TranslationStringSet
63    {
64        return new TranslationStringSet();
65    }
66
67    /**
68     * @param ReflectionClass<object> $class
69     */
70    public static function createForResourceName(ReflectionClass $class, ?BoundedContextId $boundedContextId = null): self
71    {
72        if ($boundedContextId === null) {
73            return new self('apie.resource.' . SnakeCaseSlug::fromClass($class) . '.singular');
74        }
75        return new self('apie.bounded.' .  $boundedContextId . '.resource.' . SnakeCaseSlug::fromClass($class) . '.singular');
76    }
77}