|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.