Localization   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B localize() 0 20 6
A validateNotEmpty() 0 17 4
1
<?php
2
3
namespace Asymptix\localization;
4
5
use Asymptix\web\Request;
6
use Asymptix\core\Errors;
7
8
/**
9
 * Localization class for using multiple languages and store localized lexems
10
 * into the database.
11
 *
12
 * @category Asymptix PHP Framework
13
 * @author Dmytro Zarezenko <[email protected]>
14
 * @copyright (c) 2010 - 2016, Dmytro Zarezenko
15
 *
16
 * @git https://github.com/Asymptix/Framework
17
 * @license http://opensource.org/licenses/MIT
18
 */
19
class Localization {
20
    /**
21
     * Return localized value for some value.
22
     *
23
     * @global array<mixed> $_CONFIG Global configuration.
24
     * @param string, array<string> $value Value for localization, string or
25
     *           associative array of language key and value.
26
     *
27
     * @return array<string> Localized value as associative array of string values
28
     *           for all languages.
29
     */
30
    public static function localize($value = "") {
31
        $localizedValue = Languages::$langs;
32
        if (is_array($value)) {
33
            foreach ($localizedValue as $langKey => &$lValue) {
34
                if (isset($value[$langKey])) {
35
                    $lValue = $value[$langKey];
36
                } else {
37
                    $lValue = "";
38
                }
39
            }
40
        } elseif (is_string($value)) {
41
            foreach ($localizedValue as &$lValue) {
42
                $lValue = $value;
43
            }
44
        } else {
45
            throw new Exception("Invalid value for localization");
46
        }
47
48
        return $localizedValue;
49
    }
50
51
    /**
52
     * Validate if localised field value is not empty.
53
     *
54
     * @param string $fieldName Name of the field.
55
     *
56
     * @return bool
57
     */
58
    public static function validateNotEmpty($fieldName) {
59
        $fieldValue = Request::getFieldValue($fieldName);
60
        if (is_array($fieldValue)) {
61
            foreach ($fieldValue as $value) {
62
                $value = trim($value);
63
                if (empty($value)) {
64
                    Errors::saveErrorFor($fieldName, \__ERRORS::EMPTY_FIELD);
65
66
                    return false;
67
                }
68
            }
69
        } else {
70
            return validateNotEmpty($fieldName);
71
        }
72
73
        return true;
74
    }
75
}
76