Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DefaultLabelPropertyTranslator
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 getGeneralTranslation
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 isPropertyTranslation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\Translator;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\Identifiers\CamelCaseSlug;
6use Apie\Core\Translator\Lists\TranslationStringSet;
7use Apie\Core\Translator\ValueObjects\TranslationString;
8
9/**
10 * Adds default translation for properties
11 */
12class DefaultLabelPropertyTranslator implements ApieTranslatorInterface
13{
14    public function getGeneralTranslation(ApieContext $context, TranslationString|TranslationStringSet $translations): ?string
15    {
16        if ($translations instanceof TranslationString) {
17            $translations = new TranslationStringSet([$translations]);
18        }
19        foreach ($translations as $translation) {
20            if ($this->isPropertyTranslation($translation)) {
21                return ucfirst((new CamelCaseSlug($translation->getLastTranslationSegment()))->humanize());
22            }
23        }
24        return null;
25    }
26
27    private function isPropertyTranslation(TranslationString $translation): bool
28    {
29        return (bool) preg_match('/^apie\.(bounded|resource)\..*\.properties.*(\.([a-z0-9]|__)[a-z0-9_]*)$/', $translation->toNative());
30    }
31}