Passed
Push — master ( 55c7d5...aed71d )
by Dāvis
03:01
created

BaseEntity::getVariableByLocale()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 6
nop 3
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Entity;
4
5
use Sludio\HelperBundle\Script\Utils\Helper;
6
use Sludio\HelperBundle\Translatable\Repository\TranslatableRepository as Sludio;
7
8
abstract class BaseEntity
9
{
10
    public $className;
11
    public $translates;
12
13
    public $localeArr = [
14
        'lv' => 'lv_LV',
15
        'en' => 'en_US',
16
        'ru' => 'ru_RU',
17
    ];
18
19
    abstract public function getId();
20
21
    public function __construct()
22
    {
23
        $this->translates = $this->getTranslations();
24
        $this->getClassName();
25
    }
26
27
    public function getClassName()
28
    {
29
        if (!$this->className) {
30
            $className = explode('\\', get_called_class());
31
            $this->className = strtolower(end($className));
32
        }
33
34
        return $this->className;
35
    }
36
37
    public function getLocaleVar($locale)
38
    {
39
        return isset($this->localeArr[$locale]) ? $this->localeArr[$locale] : $locale;
40
    }
41
42 View Code Duplication
    public function __get($property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        if (!method_exists($this, 'get'.ucfirst($property))) {
45
            $locale = Sludio::getDefaultLocale();
46
        } else {
47
            $locale = strtolower(substr($property, -2));
48
            $property = substr($property, 0, -2);
49
        }
50
51
        if (in_array($locale, array_keys($this->localeArr))) {
52
            return $this->getVariableByLocale($property, $this->localeArr[$locale]);
53
        }
54
55
        return $this->{$property};
56
    }
57
58 View Code Duplication
    public function __set($property, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $locale = strtolower(substr($property, -2));
61
        if (in_array($locale, array_keys($this->localeArr))) {
62
            $property = substr($property, 0, -2);
63
            Sludio::updateTranslations(get_class($this), $this->localeArr[$locale], $property, $value, $this->getId());
64
        }
65
        $this->{$property} = $value;
66
67
        return $this;
68
    }
69
70
    protected function getTranslations()
71
    {
72
        if ($this->getId()) {
73
            return Sludio::getTranslations(get_called_class(), $this->getId());
74
        }
75
    }
76
77
    public function getVariableByLocale($variable, $locale = null, $returnOriginal = false)
78
    {
79
        $locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale());
80
81
        if (!$this->translates && $this->getId()) {
82
            $this->translates = $this->getTranslations();
83
        }
84
85
        if (isset($this->translates[$locale][$variable])) {
86
            return $this->translates[$locale][$variable];
87
        }
88
89
        if ($returnOriginal) {
90
            return $this->{'get'.Helper::toCamelCase($variable)}();
91
        }
92
93
        return '';
94
    }
95
96
    public function cleanText($text)
97
    {
98
        return html_entity_decode(preg_replace('/\s+/S', ' ', str_replace(' ?', '', mb_convert_encoding(strip_tags($text), "UTF-8", "UTF-8"))));
99
    }
100
}
101