Helper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateTranslated() 0 4 1
A createTranslatedFields() 0 6 1
A separateLanguages() 0 15 4
A saveTranslatedProperties() 0 11 4
1
<?php namespace Modules\Core\Internationalisation;
2
3
/**
4
 * Class Helper
5
 * @package Modules\Core\Internationalisation
6
 */
7
class Helper
8
{
9
    /**
10
     * Save the given model properties in all given languages
11
     * @param $model
12
     * @param $data
13
     */
14
    public static function updateTranslated($model, $data)
15
    {
16
        self::saveTranslatedProperties($model, $data);
17
    }
18
19
    /**
20
     * Create the given model and save its translated attributes
21
     * @param $model
22
     * @param $data
23
     */
24
    public static function createTranslatedFields($model, $data)
25
    {
26
        $model = new $model();
27
28
        self::saveTranslatedProperties($model, $data);
29
    }
30
31
    /**
32
     * Separate the input fields into their own language key
33
     * @param $data
34
     * @return array
35
     */
36
    public static function separateLanguages($data)
37
    {
38
        $cleanedData = [];
39
        foreach ($data as $key => $value) {
40
            if (is_array($value)) {
41
                foreach ($value as $lang => $input) {
42
                    $cleanedData[$lang][$key] = $input;
43
                }
44
            } else {
45
                $cleanedData[$key] = $value;
46
            }
47
        }
48
49
        return $cleanedData;
50
    }
51
52
    /**
53
     * Save the given properties for the model
54
     * @param $model
55
     * @param $data
56
     */
57
    private static function saveTranslatedProperties($model, $data)
58
    {
59
        foreach ($data as $lang => $value) {
60
            if (is_array($value)) {
61
                foreach ($value as $key => $input) {
62
                    $model->translate($lang)->$key = $input;
63
                }
64
            }
65
        }
66
        $model->save();
67
    }
68
}
69