MultilingualHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fill() 0 7 2
A fieldsTransformer() 0 15 4
1
<?php
2
namespace Itstructure\Mult\Helpers;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Itstructure\Mult\Models\Language;
6
7
/**
8
 * Class MultilingualHelper
9
 * @package Itstructure\Mult\Helpers
10
 */
11
class MultilingualHelper
12
{
13
    /**
14
     * @param array $fields
15
     * @param callable $transformer
16
     * @param array $shortLanguageList
17
     * @return array
18
     */
19
    public static function fieldsTransformer(array $fields, callable $transformer, array $shortLanguageList = []): array
20
    {
21
        $output = [];
22
23
        if (empty($shortLanguageList)) {
24
            $shortLanguageList = Language::shortLanguageList();
25
        }
26
27
        foreach ($fields as $fieldKey => $fieldValue) {
28
            foreach ($shortLanguageList as $shortLang) {
29
                $output[$fieldKey.'_'.$shortLang] = call_user_func($transformer, $fieldValue, $shortLang);
30
            }
31
        }
32
33
        return $output;
34
    }
35
36
    /**
37
     * @param Model $model
38
     * @param array $attributes
39
     * @return Model
40
     */
41
    public static function fill(Model $model, array $attributes): Model
42
    {
43
        foreach ($attributes as $key => $value) {
44
            $model->{$key} = $value;
45
        }
46
47
        return $model;
48
    }
49
}