Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
42 / 45 |
|
78.57% |
11 / 14 |
CRAP | |
0.00% |
0 / 1 |
| TranslationStringSuffix | |
93.33% |
42 / 45 |
|
78.57% |
11 / 14 |
28.23 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPluralization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAuthenticated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withoutAuthenticated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withAuthenticated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromApieContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSimplifications | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| getSpecifity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toNative | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| createFromTranslation | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
| 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\ContextConstants; |
| 8 | use Apie\Core\Translator\Enums\Pluralization; |
| 9 | use Apie\Core\Translator\Lists\TranslationStringSuffixSet; |
| 10 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 11 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 12 | use Apie\Core\ValueObjects\Utils; |
| 13 | |
| 14 | #[Description('Contains translation string suffixes to indicate an alternate translation when not logged in or in singular or plural form. It contains the leading dot.')] |
| 15 | #[ExampleValue('.singular')] |
| 16 | #[ExampleValue('.singular.authenticated')] |
| 17 | #[ExampleValue('.singular.unauthenticated')] |
| 18 | #[ExampleValue('.plural')] |
| 19 | #[ExampleValue('.plural.authenticated')] |
| 20 | #[ExampleValue('.plural.unauthenticated')] |
| 21 | #[ExampleValue('.authenticated')] |
| 22 | #[ExampleValue('.unauthenticated')] |
| 23 | #[ExampleValue('')] |
| 24 | final class TranslationStringSuffix implements HasRegexValueObjectInterface |
| 25 | { |
| 26 | public function __construct( |
| 27 | private ?Pluralization $pluralization = null, |
| 28 | private ?bool $authenticated = null, |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | public function getPluralization(): ?Pluralization |
| 33 | { |
| 34 | return $this->pluralization; |
| 35 | } |
| 36 | |
| 37 | public function getAuthenticated(): ?bool |
| 38 | { |
| 39 | return $this->authenticated; |
| 40 | } |
| 41 | |
| 42 | public function withoutAuthenticated(): static |
| 43 | { |
| 44 | return new static($this->pluralization, null); |
| 45 | } |
| 46 | |
| 47 | public function withAuthenticated(bool $authenticated): static |
| 48 | { |
| 49 | return new static($this->pluralization, $authenticated); |
| 50 | } |
| 51 | |
| 52 | public static function fromApieContext(ApieContext $context, ?Pluralization $pluralization = null): static |
| 53 | { |
| 54 | return new static($pluralization, $context->hasContext(ContextConstants::AUTHENTICATED_USER)); |
| 55 | } |
| 56 | public function getSimplifications(): TranslationStringSuffixSet |
| 57 | { |
| 58 | $list = []; |
| 59 | if ($this->pluralization !== null) { |
| 60 | $list[] = new static(null, $this->authenticated); |
| 61 | if ($this->authenticated !== null) { |
| 62 | $list[] = new static($this->pluralization, null); |
| 63 | } |
| 64 | } elseif ($this->authenticated !== null) { |
| 65 | $list[] = new static(); |
| 66 | } |
| 67 | return new TranslationStringSuffixSet($list); |
| 68 | } |
| 69 | |
| 70 | final public function getSpecifity(): int |
| 71 | { |
| 72 | return ($this->pluralization ? 5 : 0) + ($this->authenticated ? 3 : 0); |
| 73 | } |
| 74 | |
| 75 | public function __toString(): string |
| 76 | { |
| 77 | return $this->toNative(); |
| 78 | } |
| 79 | |
| 80 | public function jsonSerialize(): mixed |
| 81 | { |
| 82 | return $this->toNative(); |
| 83 | } |
| 84 | |
| 85 | public function toNative(): string |
| 86 | { |
| 87 | $suffix = ''; |
| 88 | if ($this->pluralization) { |
| 89 | $suffix .= '.' . $this->pluralization->value; |
| 90 | } |
| 91 | if ($this->authenticated !== null) { |
| 92 | $suffix .= '.' . ($this->authenticated ? 'authenticated' : 'unauthenticated'); |
| 93 | } |
| 94 | |
| 95 | return $suffix; |
| 96 | } |
| 97 | |
| 98 | public static function getRegularExpression(): string |
| 99 | { |
| 100 | return '/^(\.(singular|plural))?(\.(authenticated|unauthenticated))?$/'; |
| 101 | } |
| 102 | |
| 103 | public static function fromNative(mixed $input): TranslationStringSuffix |
| 104 | { |
| 105 | $input = Utils::toString($input); |
| 106 | if (preg_match( |
| 107 | '/^(\.(?<plural>singular|plural))?(\.(?<auth>authenticated|unauthenticated))?$/', |
| 108 | $input, |
| 109 | $matches |
| 110 | )) { |
| 111 | return new TranslationStringSuffix( |
| 112 | empty($matches['plural']) ? null : Pluralization::from($matches['plural']), |
| 113 | empty($matches['auth']) ? null : $matches['auth'] === 'authenticated' |
| 114 | ); |
| 115 | } |
| 116 | throw new InvalidStringForValueObjectException($input, new \ReflectionClass(static::class)); |
| 117 | } |
| 118 | |
| 119 | public static function createFromTranslation(string $translationString): TranslationStringSuffix |
| 120 | { |
| 121 | if (preg_match( |
| 122 | '/(\.(?<plural>singular|plural))?(\.(?<auth>authenticated|unauthenticated))?$/', |
| 123 | $translationString, |
| 124 | $matches |
| 125 | )) { |
| 126 | return new TranslationStringSuffix( |
| 127 | empty($matches['plural']) ? null : Pluralization::from($matches['plural']), |
| 128 | empty($matches['auth']) ? null : $matches['auth'] === 'authenticated' |
| 129 | ); |
| 130 | } |
| 131 | throw new InvalidStringForValueObjectException($translationString, new \ReflectionClass(static::class)); |
| 132 | } |
| 133 | |
| 134 | } |