Passed
Push — master ( a77de0...34b538 )
by Dāvis
03:04
created

BaseEntity::getClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
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($skip = false)
26
    {
27
        return $this->getId() ? Sludio::getTranslations(get_class($this), $this->getId(), $skip) : 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
    protected function notExist($method, $pos)
59
    {
60
        return !method_exists($this, $method) && $pos !== false;
61
    }
62
63
    public function __get($property)
64
    {
65
        $getter = 'get'.ucfirst($property);
66
67
        $pos = strpos($property, '_');
68
        if ($this->notExist($getter, $pos)) {
69
            $locale = strtolower(substr($property, $pos + 1));
70
            $property = substr($property, 0, -3);
71
72
            if ($this->check($locale)) {
73
                return $this->getVariableByLocale($property, $this->getLocaleVar($locale));
74
            }
75
        }
76
77
        return $this->{$getter}();
78
    }
79
80
    public function __set($property, $value)
81
    {
82
        $pos = strpos($property, '_');
83
        $setter = 'set'.ucfirst($property);
84
85
        if ($this->notExist($setter, $pos)) {
86
            $locale = strtolower(substr($property, $pos + 1));
87
            $property = substr($property, 0, -3);
88
89
            if ($this->check($locale)) {
90
                Sludio::updateTranslations(get_class($this), $this->getLocaleVar($locale), $property, $value, $this->getId());
91
                $this->getTranslations();
92
            }
93
        }
94
        $this->{$property} = $value;
95
96
        return $this;
97
    }
98
99
    public function getVariableByLocale($variable, $locale = null, $returnOriginal = false)
100
    {
101
        $locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale());
102
103
        if (!$this->translates && $this->getId()) {
104
            $this->translates = $this->getTranslations();
105
        }
106
107
        if (isset($this->translates[$locale][$variable])) {
108
            return $this->translates[$locale][$variable];
109
        }
110
111
        if ($returnOriginal) {
112
            return $this->{'get'.Helper::toCamelCase($variable)}();
113
        }
114
115
        return '';
116
    }
117
118
    public function getLocaleVar($locale)
119
    {
120
        return $this->check($locale) ? $this->localeArr[$locale] : $locale;
121
    }
122
123
    public function cleanText($text)
124
    {
125
        return Helper::cleanText($text);
126
    }
127
}
128