Passed
Push — master ( 803693...151800 )
by Dāvis
03:44
created

BaseEntity::__get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 8
Ratio 53.33 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 8
loc 15
rs 9.2
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
    public function __construct()
20
    {
21
        $this->translates = $this->getTranslations();
22
        $this->getClassName();
23
    }
24
25
    protected function getTranslations()
26
    {
27
        return $this->getId() ? Sludio::getTranslations(get_class($this), $this->getId()) : null;
28
    }
29
30
    abstract public function getId();
31
32
    public function getClassName()
33
    {
34
        if (!$this->className) {
35
            $className = explode('\\', get_called_class());
36
            $this->className = strtolower(end($className));
37
        }
38
39
        return $this->className;
40
    }
41
42
    public function __call($name, $arguments)
43
    {
44
        $pos = strpos($name, '_');
45
        if ($pos !== false) {
46
            $locale = strtolower(substr($name, $pos + 1));
47
            if (count($arguments) === 0 && $this->check($locale) === true) {
48
                return $this->__get($name);
49
            }
50
        }
51
    }
52
53
    protected function check($locale)
54
    {
55
        return in_array($locale, array_keys($this->localeArr));
56
    }
57
58
    public function __get($property)
59
    {
60
        $getter = 'get'.ucfirst($property);
61
62
        $pos = strpos($property, '_');
63 View Code Duplication
        if (!method_exists($this, $getter) && $pos !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
64
            $locale = strtolower(substr($property, $pos + 1));
65
            $property = substr($property, 0, -3);
66
67
            if ($this->check($locale)) {
68
                return $this->getVariableByLocale($property, $this->getLocaleVar($locale));
69
            }
70
        }
71
72
        return $this->{$getter}();
73
    }
74
75
    public function __set($property, $value)
76
    {
77
        $pos = strpos($property, '_');
78
        $setter = 'set'.ucfirst($property);
79
80 View Code Duplication
        if (!method_exists($this, $setter) && $pos !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
            $locale = strtolower(substr($property, $pos + 1));
82
            $property = substr($property, 0, -3);
83
84
            if ($this->check($locale)) {
85
                Sludio::updateTranslations(get_class($this), $this->getLocaleVar($locale), $property, $value, $this->getId());
86
            }
87
        }
88
        $this->{$property} = $value;
89
90
        return $this;
91
    }
92
93
    public function getVariableByLocale($variable, $locale = null, $returnOriginal = false)
94
    {
95
        $locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale());
96
97
        if (!$this->translates && $this->getId()) {
98
            $this->translates = $this->getTranslations();
99
        }
100
101
        if (isset($this->translates[$locale][$variable])) {
102
            return $this->translates[$locale][$variable];
103
        }
104
105
        if ($returnOriginal) {
106
            return $this->{'get'.Helper::toCamelCase($variable)}();
107
        }
108
109
        return '';
110
    }
111
112
    public function getLocaleVar($locale)
113
    {
114
        return $this->check($locale) ? $this->localeArr[$locale] : $locale;
115
    }
116
117
    public function cleanText($text)
118
    {
119
        return Helper::cleanText($text);
120
    }
121
}
122