OptionField::make()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Oscer\Cms\Backend\Resources\Fields;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
class OptionField
10
{
11
    public static function make(string $key, string $type)
12
    {
13
        [$tab, $label] = explode('.', $key);
0 ignored issues
show
Bug introduced by
The variable $tab does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $label does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
14
15
        $resolveValueCallback = function (Field $field) {
16
            // We access the value property of the Option model
17
            return $field->model->value;
18
        };
19
20
        $fillResourceCallback = function (Model $model, Request $request) {
21
            // We need to replace the "." with an "_" because dot notation acts like array access
22
            $value = $request->input(Str::replaceFirst('.', '_', $model->key));
23
            // We Write the value into the value property of the Option model
24
            $model->value = $value;
25
        };
26
27
        switch ($type) {
28
            case 'text':
29
                return TextField::make($key, $label, $resolveValueCallback, $fillResourceCallback);
30
            default:
31
                throw new \Exception('unknown field type');
32
        }
33
    }
34
}
35