LanguageModel::getCodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Model;
13
14
use Jitamin\Foundation\Database\Model;
15
use Jitamin\Foundation\Translator;
16
17
/**
18
 * Class Language.
19
 */
20
class LanguageModel extends Model
21
{
22
    /**
23
     * Get all language codes.
24
     *
25
     * @static
26
     *
27
     * @return string[]
28
     */
29
    public static function getCodes()
30
    {
31
        return [
32
            'en_US',
33
            'zh_CN',
34
        ];
35
    }
36
37
    /**
38
     * Find language code.
39
     *
40
     * @static
41
     *
42
     * @param string $code
43
     *
44
     * @return string
45
     */
46
    public static function findCode($code)
47
    {
48
        $code = str_replace('-', '_', $code);
49
50
        return in_array($code, self::getCodes()) ? $code : '';
51
    }
52
53
    /**
54
     * Get available languages.
55
     *
56
     * @param bool $prepend Prepend a default value
57
     *
58
     * @return array
59
     */
60
    public function getLanguages($prepend = false)
61
    {
62
        // Sorted by value
63
        $languages = [
64
            'en_US' => 'English',
65
            'zh_CN' => '中文(简体)',
66
        ];
67
68
        if ($prepend) {
69
            return ['' => t('Use system language')] + $languages;
70
        }
71
72
        return $languages;
73
    }
74
75
    /**
76
     * Get javascript language code.
77
     *
78
     * @return string
79
     */
80
    public function getJsLanguageCode()
81
    {
82
        $languages = [
83
            'en_US' => 'en',
84
            'zh_CN' => 'zh-cn',
85
        ];
86
87
        $lang = $this->getCurrentLanguage();
88
89
        return isset($languages[$lang]) ? $languages[$lang] : 'en';
90
    }
91
92
    /**
93
     * Get current language.
94
     *
95
     * @return string
96
     */
97 View Code Duplication
    public function getCurrentLanguage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
    {
99
        if ($this->userSession->isLogged() && !empty($this->sessionStorage->user['language'])) {
0 ignored issues
show
Documentation introduced by
The property userSession does not exist on object<Jitamin\Model\LanguageModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Model\LanguageModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
            return $this->sessionStorage->user['language'];
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Model\LanguageModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
        }
102
103
        return $this->settingModel->get('application_language', 'en_US');
0 ignored issues
show
Documentation introduced by
The property settingModel does not exist on object<Jitamin\Model\LanguageModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
    }
105
106
    /**
107
     * Load translations for the current language.
108
     */
109
    public function loadCurrentLanguage()
110
    {
111
        Translator::load($this->getCurrentLanguage());
112
    }
113
}
114