Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.67% |
51 / 90 |
|
27.27% |
3 / 11 |
CRAP | |
0.00% |
0 / 1 |
| TranslationStringSetBuilder | |
56.67% |
51 / 90 |
|
27.27% |
3 / 11 |
75.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| buildFromSegments | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
8.10 | |||
| build | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| makeSet | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
| makeAllVariations | |
90.48% |
19 / 21 |
|
0.00% |
0 / 1 |
4.01 | |||
| singular | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| plural | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| withProperty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| withPlaceholder | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| withOperationType | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Identifiers\SnakeCaseSlug; |
| 6 | use Apie\Core\Translator\Enums\TranslationStringOperationType; |
| 7 | use Apie\Core\Translator\Enums\TranslationStringType; |
| 8 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 9 | use Apie\Core\Translator\ValueObjects\TranslationString; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionProperty; |
| 12 | |
| 13 | final class TranslationStringSetBuilder |
| 14 | { |
| 15 | private ?TranslationStringType $translationStringType = null; |
| 16 | |
| 17 | private ?ReflectionProperty $property = null; |
| 18 | |
| 19 | private ?TranslationStringOperationType $operationType = null; |
| 20 | |
| 21 | /** |
| 22 | * @param ReflectionClass<object> $resourceName |
| 23 | */ |
| 24 | private function __construct( |
| 25 | private ReflectionClass $resourceName, |
| 26 | private ?BoundedContextId $boundedContextId = null |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param ReflectionClass<object> $resourceName |
| 32 | */ |
| 33 | public static function create(ReflectionClass $resourceName, ?BoundedContextId $boundedContextId = null): self |
| 34 | { |
| 35 | return new self($resourceName, $boundedContextId); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param array<int, string|array<int, string>> $segments |
| 40 | * @return array<int, string> |
| 41 | */ |
| 42 | private function buildFromSegments(array $segments): array |
| 43 | { |
| 44 | $first = array_shift($segments); |
| 45 | if ($first === null) { |
| 46 | return []; |
| 47 | } |
| 48 | if (is_string($first)) { |
| 49 | $res = []; |
| 50 | if (empty($segments)) { |
| 51 | return [$first]; |
| 52 | } |
| 53 | foreach ($this->buildFromSegments($segments) as $segment) { |
| 54 | $res[] = $first . $segment; |
| 55 | } |
| 56 | return $res; |
| 57 | } |
| 58 | $res = []; |
| 59 | if (empty($segments)) { |
| 60 | return $first; |
| 61 | } |
| 62 | foreach ($this->buildFromSegments($segments) as $segment) { |
| 63 | foreach ($first as $prefix) { |
| 64 | $res[] = $prefix . $segment; |
| 65 | } |
| 66 | } |
| 67 | return $res; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param array<int, array<int, string|array<int, string>>> $allSegments |
| 72 | */ |
| 73 | private function build(array... $allSegments): TranslationStringSet |
| 74 | { |
| 75 | return new TranslationStringSet( |
| 76 | array_map( |
| 77 | function (string $value) { |
| 78 | return TranslationString::fromNative($value); |
| 79 | }, |
| 80 | array_merge( |
| 81 | ...array_map( |
| 82 | function (array $segments) { |
| 83 | return $this->buildFromSegments($segments); |
| 84 | }, |
| 85 | $allSegments |
| 86 | ) |
| 87 | ) |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | public function makeSet(): TranslationStringSet |
| 93 | { |
| 94 | if ($this->translationStringType === null) { |
| 95 | throw new \LogicException( |
| 96 | 'No operation type is set, please call singular(), plural(), withProperty() or withPlaceholder()' |
| 97 | ); |
| 98 | } |
| 99 | $segments = [ |
| 100 | 'apie.', |
| 101 | $this->boundedContextId ? ['bounded.' . $this->boundedContextId . '.', ''] : '', |
| 102 | 'resource.', |
| 103 | SnakeCaseSlug::fromClass($this->resourceName)->toNative(), |
| 104 | '.', |
| 105 | $this->operationType ? $this->operationType->value : 'general', |
| 106 | '.', |
| 107 | $this->translationStringType->value, |
| 108 | $this->property ? ('.' . SnakeCaseSlug::fromClass($this->property)) : '', |
| 109 | ]; |
| 110 | // @phpstan-ignore argument.type |
| 111 | return $this->build($segments); |
| 112 | } |
| 113 | |
| 114 | public function makeAllVariations(): TranslationStringSet |
| 115 | { |
| 116 | $operationTypes = $this->operationType |
| 117 | ? [$this->operationType->value] |
| 118 | : TranslationStringOperationType::stringCases(); |
| 119 | $allSegments = array_map( |
| 120 | function (string $operationType) { |
| 121 | return [ |
| 122 | 'apie.', |
| 123 | $this->boundedContextId ? ['bounded.' . $this->boundedContextId . '.', ''] : '', |
| 124 | 'resource.', |
| 125 | SnakeCaseSlug::fromClass($this->resourceName)->toNative(), |
| 126 | '.', |
| 127 | $operationType, |
| 128 | '.', |
| 129 | $this->translationStringType |
| 130 | ? $this->translationStringType->value |
| 131 | : TranslationStringType::stringCasesFor($this->resourceName, TranslationStringOperationType::tryFrom($operationType)), |
| 132 | ]; |
| 133 | }, |
| 134 | $operationTypes |
| 135 | ); |
| 136 | // @phpstan-ignore argument.type |
| 137 | return $this->build(...$allSegments); |
| 138 | } |
| 139 | |
| 140 | public function singular(): self |
| 141 | { |
| 142 | $clone = clone $this; |
| 143 | $clone->translationStringType = TranslationStringType::Singular; |
| 144 | $clone->property = null; |
| 145 | return $clone; |
| 146 | } |
| 147 | |
| 148 | public function plural(): self |
| 149 | { |
| 150 | $clone = clone $this; |
| 151 | $clone->translationStringType = TranslationStringType::Plural; |
| 152 | $clone->property = null; |
| 153 | return $clone; |
| 154 | } |
| 155 | |
| 156 | public function withProperty(ReflectionProperty $property): self |
| 157 | { |
| 158 | $clone = clone $this; |
| 159 | $clone->translationStringType = TranslationStringType::Properties; |
| 160 | $clone->property = $property; |
| 161 | return $clone; |
| 162 | } |
| 163 | |
| 164 | public function withPlaceholder(ReflectionProperty $property): self |
| 165 | { |
| 166 | $clone = clone $this; |
| 167 | $clone->translationStringType = TranslationStringType::Placeholders; |
| 168 | $clone->property = $property; |
| 169 | return $clone; |
| 170 | } |
| 171 | |
| 172 | public function withOperationType(TranslationStringOperationType $operationType): self |
| 173 | { |
| 174 | $clone = clone $this; |
| 175 | $clone->operationType = $operationType; |
| 176 | return $clone; |
| 177 | } |
| 178 | } |