Test Failed
Push — master ( b1cf76...d8fe36 )
by Julien
08:28
created

Locale   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 31
dl 0
loc 97
ccs 20
cts 32
cp 0.625
rs 10
c 1
b 1
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 16 3
A __get() 0 16 3
A __set() 0 17 3
A _() 0 6 1
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\Traits;
13
14
use Phalcon\Mvc\Model\Exception;
15
use Phalcon\Translate\Adapter\AbstractAdapter;
16
use Zemit\Mvc\Model\Traits\Abstracts\AbstractEntity;
17
use Zemit\Mvc\Model\Traits\Abstracts\AbstractInjectable;
18
19
/**
20
 * This trait provides functionality to handle localization in models.
21
 */
22
trait Locale
23
{
24
    use AbstractInjectable;
25
    use AbstractEntity;
26
    
27
    /**
28
     * Translate a given key using the translation service
29
     *
30
     * @param string $translateKey The key to be translated
31
     * @param array $placeholders The placeholders to be replaced in the translation
32
     * @return string The translated string
33
     */
34 3
    public function _(string $translateKey, array $placeholders = []): string
35
    {
36 3
        $translate = $this->getDI()->get('translate');
37 3
        assert($translate instanceof AbstractAdapter);
38
        
39 3
        return $translate->_($translateKey, $placeholders);
40
    }
41
    
42
    /**
43
     * Magic method to dynamically call localed named methods using the current locale
44
     * - Allow to call $this->methodName{Fr|En|Sp|...}() from missing methodName method
45
     *
46
     * @param string $method method name
47
     * @param array $arguments method arguments
48
     * @return mixed|null
49
     * @throws Exception
50
     */
51
    public function __call(string $method, array $arguments): mixed
52
    {
53
        $locale = $this->getDI()->get('locale');
54
        assert($locale instanceof \Zemit\Locale);
55
        
56
        $lang = $locale->getLocale();
57
        
58
        if (!empty($lang)) {
59
            $call = $method . ucfirst($lang);
60
            if (method_exists($this, $call)) {
61
                return $this->$call(...$arguments);
62
            }
63
        }
64
        
65
//        return $this->$method(...$arguments);
66
        return parent::__call($method, $arguments);
67
    }
68
    
69
    /**
70
     * Magic setter to set localed named field automatically using the current locale
71
     * - Allow to set $this->name{Fr|En|Sp|...} for missing name property
72
     *
73
     * @param string $property property name
74
     * @param mixed $value value to be set for the property
75
     * @return void
76
     */
77 3
    public function __set(string $property, mixed $value): void
78
    {
79 3
        $locale = $this->getDI()->get('locale');
80 3
        assert($locale instanceof \Zemit\Locale);
81
        
82 3
        $lang = $locale->getLocale();
83
        
84 3
        if (!empty($lang)) {
85 3
            $set = $property . ucfirst($lang);
86
            
87 3
            if (property_exists($this, $set)) {
88
                $this->writeAttribute($set, $value);
89
                return;
90
            }
91
        }
92
        
93 3
        parent::__set($property, $value);
94
    }
95
    
96
    /**
97
     * Magic getter to get localed named field automatically using the current locale
98
     * - Allow to get $this->name{Fr|En|Sp|...} from missing name property
99
     *
100
     * @param string $property property name
101
     * @return mixed
102
     */
103 3
    public function __get(string $property): mixed
104
    {
105 3
        $locale = $this->getDI()->get('locale');
106 3
        assert($locale instanceof \Zemit\Locale);
107
        
108 3
        $lang = $locale->getLocale();
109
        
110 3
        if (!empty($lang)) {
111 3
            $get = $property . ucfirst($lang);
112
            
113 3
            if (property_exists($this, $get)) {
114
                return $this->readAttribute($get);
115
            }
116
        }
117
        
118 3
        return parent::__get($property);
119
    }
120
}
121