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 array_key_exists($locale, $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 __isset($name) |
81
|
|
|
{ |
82
|
|
|
$getter = 'get'.ucfirst($name); |
83
|
|
|
|
84
|
|
|
return method_exists($this, $getter) && $this->$getter() !== null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __set($property, $value) |
88
|
|
|
{ |
89
|
|
|
$pos = strpos($property, '_'); |
90
|
|
|
$setter = 'set'.ucfirst($property); |
91
|
|
|
|
92
|
|
|
if ($this->notExist($setter, $pos)) { |
93
|
|
|
$locale = strtolower(substr($property, $pos + 1)); |
94
|
|
|
$property = substr($property, 0, -3); |
95
|
|
|
|
96
|
|
|
if ($this->check($locale)) { |
97
|
|
|
Sludio::updateTranslations(\get_class($this), $this->getLocaleVar($locale), $property, $value, $this->getId()); |
98
|
|
|
$this->getTranslations(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
$this->{$property} = $value; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getVariableByLocale($variable, $locale = null, $returnOriginal = false) |
107
|
|
|
{ |
108
|
|
|
$locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale()); |
109
|
|
|
|
110
|
|
|
if (!$this->translates && $this->getId()) { |
111
|
|
|
$this->translates = $this->getTranslations(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (isset($this->translates[$locale][$variable])) { |
115
|
|
|
return $this->translates[$locale][$variable]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($returnOriginal) { |
119
|
|
|
return $this->{'get'.Helper::toCamelCase($variable)}(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getLocaleVar($locale) |
126
|
|
|
{ |
127
|
|
|
return $this->check($locale) ? $this->localeArr[$locale] : $locale; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function cleanText($text) |
131
|
|
|
{ |
132
|
|
|
return Helper::cleanText($text); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|