Passed
Branch master (a0cc06)
by Dāvis
17:06
created

BaseEntity::getVariableByLocale()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 16
nop 3
dl 0
loc 29
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Entity;
4
5
use Sludio\HelperBundle\Translatable\Repository\TranslatableRepository as Sludio;
6
7
class BaseEntity
8
{
9
    public $className;
10
    public $translates;
11
12
    public $localeArr = [
13
        'lv' => 'lv_LV',
14
        'en' => 'en_US',
15
        'ru' => 'ru_RU',
16
    ];
17
18
    public function __construct()
19
    {
20
        if (method_exists($this, 'getId') && $this->getId()) {
21
            $this->translates = $this->getTranslations();
22
        }
23
        $this->getClassName();
24
    }
25
26
    public function getClassName()
27
    {
28
        if (!$this->className) {
29
            $className = explode('\\', get_called_class());
30
            $this->className = strtolower(end($className));
31
        }
32
33
        return $this->className;
34
    }
35
36
    public function getLocaleVar($locale)
37
    {
38
        return $this->localeArr[$locale];
39
    }
40
41 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...
42
    {
43
        if (!method_exists($this, 'get'.ucfirst($property))) {
44
            $locale = Sludio::getDefaultLocale();
45
        } else {
46
            $locale = strtolower(substr($property, -2));
47
            $property = substr($property, 0, -2);
48
        }
49
50
        if (in_array($locale, array_keys($this->localeArr))) {
51
            return $this->getVariableByLocale($property, $this->localeArr[$locale]);
52
        }
53
54
        return $this->{$property};
55
    }
56
57 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...
58
    {
59
        $locale = strtolower(substr($property, -2));
60
        if (in_array($locale, array_keys($this->localeArr))) {
61
            $property = substr($property, 0, -2);
62
            Sludio::updateTranslations(get_called_class(), $this->localeArr[$locale], $property, $value, $this->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Sludio\HelperBundle\Translatable\Entity\BaseEntity. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            Sludio::updateTranslations(get_called_class(), $this->localeArr[$locale], $property, $value, $this->/** @scrutinizer ignore-call */ getId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
        }
64
        $this->{$property} = $value;
65
66
        return $this;
67
    }
68
69
    protected function getTranslations()
70
    {
71
        return Sludio::getTranslations(get_called_class(), $this->getId());
72
    }
73
74
    public function getVariableByLocale($variable, $locale = null, $return_original = false)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $return_original is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
75
    {
76
        $locale = $locale ?: Sludio::getDefaultLocale();
77
78
        if (!$this->translates && $this->getId()) {
79
            $this->translates = $this->getTranslations();
80
        }
81
82
        if (strlen($locale) == 2) {
83
            $locale = $this->localeArr[$locale];
84
        }
85
86
        if (isset($this->translates[$locale][$variable])) {
87
            return $this->translates[$locale][$variable];
88
        }
89
90
        if ($return_original) {
91
            $variables = explode('_', $variable);
92
            foreach ($variables as &$v) {
93
                $v = ucfirst($v);
94
            }
95
            $variable = implode('', $variables);
96
            $res = $this->{'get'.$variable}();
97
            if (is_numeric($res)) {
98
                return $res;
99
            }
100
        }
101
102
        return '';
103
    }
104
105
    public function cleanText($text)
106
    {
107
        $text = strip_tags($text);
108
        $text = mb_convert_encoding($text, "UTF-8", "UTF-8");
109
        $text = str_replace(' ?', '', $text);
110
111
        return $text;
112
    }
113
}
114