1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Model\Exception; |
15
|
|
|
use Phalcon\Translate\Adapter\AbstractAdapter; |
16
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable; |
17
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractEntity; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait Locale |
21
|
|
|
* |
22
|
|
|
* This trait provides functionality to handle localization in models. |
23
|
|
|
*/ |
24
|
|
|
trait Locale |
25
|
|
|
{ |
26
|
|
|
use AbstractInjectable; |
27
|
|
|
use AbstractEntity; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Translate a given key using the translation service |
31
|
|
|
* |
32
|
|
|
* @param string $translateKey The key to be translated |
33
|
|
|
* @param array $placeholders The placeholders to be replaced in the translation |
34
|
|
|
* @return string The translated string |
35
|
|
|
*/ |
36
|
2 |
|
public function _(string $translateKey, array $placeholders = []): string |
37
|
|
|
{ |
38
|
2 |
|
$translate = $this->getDI()->get('translate'); |
39
|
2 |
|
assert($translate instanceof AbstractAdapter); |
40
|
|
|
|
41
|
2 |
|
return $translate->_($translateKey, $placeholders); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Magic method to dynamically call localed named methods using the current locale |
46
|
|
|
* - Allow to call $this->methodName{Fr|En|Sp|...}() from missing methodName method |
47
|
|
|
* |
48
|
|
|
* @param string $method method name |
49
|
|
|
* @param array $arguments method arguments |
50
|
|
|
* @return mixed|null |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
|
public function __call(string $method, array $arguments): mixed |
54
|
|
|
{ |
55
|
|
|
$locale = $this->getDI()->get('locale'); |
56
|
|
|
assert($locale instanceof \Zemit\Locale); |
57
|
|
|
|
58
|
|
|
$lang = $locale->getLocale(); |
59
|
|
|
|
60
|
|
|
if (!empty($lang)) { |
61
|
|
|
$call = $method . ucfirst($lang); |
62
|
|
|
if (method_exists($this, $call)) { |
63
|
|
|
return $this->$call(...$arguments); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// return $this->$method(...$arguments); |
68
|
|
|
return @parent::__call($method, $arguments); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Magic setter to set localed named field automatically using the current locale |
73
|
|
|
* - Allow to set $this->name{Fr|En|Sp|...} for missing name property |
74
|
|
|
* |
75
|
|
|
* @param string $property property name |
76
|
|
|
* @param mixed $value value to be set for the property |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function __set(string $property, mixed $value): void |
80
|
|
|
{ |
81
|
|
|
$locale = $this->getDI()->get('locale'); |
82
|
|
|
assert($locale instanceof \Zemit\Locale); |
83
|
|
|
|
84
|
|
|
$lang = $locale->getLocale(); |
85
|
|
|
|
86
|
|
|
if (!empty($lang)) { |
87
|
|
|
$set = $property . ucfirst($lang); |
88
|
|
|
|
89
|
|
|
if (property_exists($this, $set)) { |
90
|
|
|
$this->writeAttribute($set, $value); |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
@parent::__set($property, $value); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Magic getter to get localed named field automatically using the current locale |
100
|
|
|
* - Allow to get $this->name{Fr|En|Sp|...} from missing name property |
101
|
|
|
* |
102
|
|
|
* @param string $property property name |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function __get(string $property): mixed |
106
|
|
|
{ |
107
|
|
|
$locale = $this->getDI()->get('locale'); |
108
|
|
|
assert($locale instanceof \Zemit\Locale); |
109
|
|
|
|
110
|
|
|
$lang = $locale->getLocale(); |
111
|
|
|
|
112
|
|
|
if (!empty($lang)) { |
113
|
|
|
$get = $property . ucfirst($lang); |
114
|
|
|
|
115
|
|
|
if (property_exists($this, $get)) { |
116
|
|
|
return $this->readAttribute($get); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return @parent::__get($property); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: