Test Failed
Push — master ( 7665f3...4425a6 )
by Julien
04:44
created

Locale::_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
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
trait Locale
15
{
16
    /**
17
     * Returns the translation string of the given key
18
     *
19
     * @param array $placeholders
20
     * @param string $translateKey
21
     * @return string
22
     */
23
    public function _(string $translateKey, array $placeholders = []): string
24
    {
25
        $translate = $this->getDI()->get('translate');
0 ignored issues
show
Bug introduced by
The method getDI() does not exist on Zemit\Mvc\Model\Locale. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        $translate = $this->/** @scrutinizer ignore-call */ getDI()->get('translate');
Loading history...
26
        assert($translate instanceof \Phalcon\Translate\Adapter\AbstractAdapter);
27
        
28
        return $translate->_($translateKey, $placeholders);
29
    }
30
    
31
    /**
32
     * Magic caller to set or get localed named field automagically using the current locale
33
     * - Allow to call $this->getName{Fr|En|Sp|...}
34
     * - Allow to call $this->setName{Fr|En|Sp|...}
35
     *
36
     * @param string $method method name
37
     * @param array $arguments method arguments
38
     * @return mixed
39
     * @throws \Phalcon\Mvc\Model\Exception
40
     */
41
    public function __call(string $method, array $arguments)
42
    {
43
        $locale = $this->getDI()->get('locale');
44
        assert($locale instanceof \Zemit\Locale);
45
        
46
        $lang = $locale->getLocale() ?: 'en';
47
        
48
        if (mb_strrpos($method, ucfirst($lang)) !== mb_strlen($method) - mb_strlen($lang)) {
49
            $call = $method . ucfirst($lang);
50
            if (method_exists($this, $call)) {
51
                return $this->$call(...$arguments);
52
            }
53
        }
54
        
55
        return parent::__call($method, $arguments);
56
    }
57
    
58
    /**
59
     * Magic setter to set localed named field automatically using the current locale
60
     * - Allow to set $this->name{Fr|En|Sp|...} from missing name property
61
     *
62
     * @param string $property property name
63
     * @param mixed $value value to set
64
     * @return void
65
     */
66
    public function __set(string $property, $value)
67
    {
68
        $locale = $this->getDI()->get('locale');
69
        assert($locale instanceof \Zemit\Locale);
70
        
71
        $lang = $locale->getLocale();
72
        
73
        if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) {
0 ignored issues
show
Bug introduced by
It seems like $lang can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        if (mb_strrpos($property, ucfirst(/** @scrutinizer ignore-type */ $lang)) !== mb_strlen($property) - 2) {
Loading history...
74
            $set = $property . ucfirst($lang);
75
            
76
            if (property_exists($this, $set)) {
77
                $this->writeAttribute($set, $value);
0 ignored issues
show
Bug introduced by
The method writeAttribute() does not exist on Zemit\Mvc\Model\Locale. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                $this->/** @scrutinizer ignore-call */ 
78
                       writeAttribute($set, $value);
Loading history...
78
                
79
                return;
80
            }
81
        }
82
        
83
        parent::__set($property, $value);
84
    }
85
    
86
    /**
87
     * Magic getter to get localed named field automatically using the current locale
88
     * - Allow to get $this->name{Fr|En|Sp|...} from missing name property
89
     *
90
     * @param string $property property name
91
     * @return mixed|null
92
     */
93
    public function __get(string $property)
94
    {
95
        $locale = $this->getDI()->get('locale');
96
        assert($locale instanceof \Zemit\Locale);
97
        
98
        $lang = $locale->getLocale();
99
        
100
        if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) {
0 ignored issues
show
Bug introduced by
It seems like $lang can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        if (mb_strrpos($property, ucfirst(/** @scrutinizer ignore-type */ $lang)) !== mb_strlen($property) - 2) {
Loading history...
101
            $set = $property . ucfirst($lang);
102
            
103
            if (property_exists($this, $set)) {
104
                return $this->readAttribute($set);
0 ignored issues
show
Bug introduced by
The method readAttribute() does not exist on Zemit\Mvc\Model\Locale. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
                return $this->/** @scrutinizer ignore-call */ readAttribute($set);
Loading history...
105
            }
106
        }
107
        
108
        return parent::__get($property);
109
    }
110
}
111