Passed
Pull Request — 2.x (#1151)
by
unknown
06:31 queued 11s
created

HandleFieldsGroups::getModelCasts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
trait HandleFieldsGroups
9
{
10
    /**
11
     * @param \A17\Twill\Models\Model $object
12
     * @param array|null $fields
13
     * @return \A17\Twill\Models\Model
14
     */
15 3
    public function hydrateHandleFieldsGroups($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

15
    public function hydrateHandleFieldsGroups($object, /** @scrutinizer ignore-unused */ $fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17 3
        foreach ($this->fieldsGroups as $group => $groupFields) {
18
            if ($object->$group) {
19
                $casts = $this->getModelCasts($object);
20
                if (!array_key_exists($group, $casts) || (array_key_exists($group, $casts) && $casts[$group] !== 'array')) {
21
                    $object->$group = json_encode($object->$group);
22
                }
23 3
            }
24
        }
25
26
        return $object;
27
    }
28
29
    /**
30
     * @param \A17\Twill\Models\Model|null $object
31 31
     * @param array $fields
32
     * @return array
33 31
     */
34
    public function prepareFieldsBeforeSaveHandleFieldsGroups($object, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

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

34
    public function prepareFieldsBeforeSaveHandleFieldsGroups(/** @scrutinizer ignore-unused */ $object, $fields)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        return $this->handleFieldsGroups($fields);
37
    }
38
39
    /**
40
     * @param array $fields
41 26
     * @return array
42
     */
43 26
    public function prepareFieldsBeforeCreateHandleFieldsGroups($fields)
44
    {
45
        return $this->handleFieldsGroups($fields);
46
    }
47
48
    /**
49
     * @param \A17\Twill\Models\Model $object
50
     * @param array $fields
51 6
     * @return array
52
     */
53 6
    public function getFormFieldsHandleFieldsGroups($object, $fields)
54
    {
55
        foreach ($this->fieldsGroups as $group => $groupFields) {
56
            if ($object->$group) {
57
                $casts = $this->getModelCasts($object);
58
                if (array_key_exists($group, $casts) && $casts[$group] === 'array') {
59
                    $decoded_fields = $object->$group;
60
                } else {
61
                    $decoded_fields = json_decode($object->$group, true) ?? [];
62
                }
63
64 6
                // In case that some field read the value through $item->$name
65
                foreach ($decoded_fields as $field_name => $field_value) {
66
                    if ($this->fieldsGroupsFormFieldNameSeparator !== null) {
67 32
                        $decoded_fields[$group . $this->fieldsGroupsFormFieldNameSeparator . $field_name] = $field_value;
68 32
                        unset($decoded_fields[$field_name]);
69
70
                        $object->setAttribute($group . $this->fieldsGroupsFormFieldNameSeparator . $field_name, $field_value);
71
                    } else {
72
                        $object->setAttribute($field_name, $field_value);
73
                    }
74
                }
75
                $fields = array_merge($fields, $decoded_fields);
76
            }
77
        }
78
79
        return $fields;
80 32
    }
81
82
    protected function handleFieldsGroups($fields)
83
    {
84
        foreach ($this->fieldsGroups as $group => $groupFields) {
85
            if ($this->fieldsGroupsFormFieldNameSeparator !== null) {
86
                $groupFields = array_map(function ($field_name) use ($group) {
87
                    return $group . $this->fieldsGroupsFormFieldNameSeparator . $field_name;
88
                }, $groupFields);
89
            }
90
91
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

91
            $fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
                return !empty($value);
93
            });
94
95
            if ($this->fieldsGroupsFormFieldNameSeparator !== null) {
96
                $fieldsGroupWithGroupSeparator = [];
97
                foreach ($fields[$group] as $key => $value) {
98
                    $fieldsGroupWithGroupSeparator[Str::replaceFirst($group . $this->fieldsGroupsFormFieldNameSeparator, '', $key)] = $value;
99
                }
100
                $fields[$group] = $fieldsGroupWithGroupSeparator;
101
            }
102
103
            if (in_array($group, $this->model->translatedAttributes) && is_array($fields[$group])) {
104
                $fieldForTranslationTrait = [];
105
                foreach ($fields[$group] as $field => $translatedValues) {
106
                    foreach ($translatedValues as $locale => $value) {
107
                        $fieldForTranslationTrait[$locale][$field] = $value;
108
                    }
109
                }
110
                $fields[$group] = $fieldForTranslationTrait;
111
            }
112
113
            if (empty($fields[$group])) {
114
                $fields[$group] = null;
115
            }
116
117
            Arr::forget($fields, $groupFields);
118
        }
119
120
        return $fields;
121
    }
122
123
    /**
124
     * @param \A17\Twill\Models\Model $object
125
     * @return array
126
     */
127
    protected function getModelCasts($object)
128
    {
129
        $casts = $object->getCasts();
130
        if ($this->model->isTranslatable()) {
131
            $casts = $casts + app()->make($this->model->getTranslationModelNameDefault())->getCasts();
132
        }
133
134
        return $casts;
135
    }
136
}
137