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