Completed
Push — master ( e4fa3f...8b68bb )
by Song
02:50
created

Table::getKeyName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\NestedForm;
6
7
class Table extends HasMany
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $view = 'admin::form.hasmanytable';
13
14
    /**
15
     * Table constructor.
16
     *
17
     * @param string $column
18
     * @param array $arguments
19
     */
20
    public function __construct($column, $arguments = [])
21
    {
22
        $this->column = $column;
23
24
        if (count($arguments) == 1) {
25
            $this->label = $this->formatLabel();
26
            $this->builder = $arguments[0];
27
        }
28
29
        if (count($arguments) == 2) {
30
            list($this->label, $this->builder) = $arguments;
31
        }
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    protected function buildRelatedForms()
38
    {
39
        if (is_null($this->form)) {
40
            return [];
41
        }
42
43
        $forms = [];
44
45
        if ($values = old($this->column)) {
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, old() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
            foreach ($values as $key => $data) {
47
                if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) {
48
                    continue;
49
                }
50
51
                $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
52
            }
53
        } else {
54
            foreach ($this->value as $key => $data) {
55
                $forms[$key] = $this->buildNestedForm($this->column, $this->builder, $key)->fill($data);
56
            }
57
        }
58
59
        return $forms;
60
    }
61
62
    public function prepare($input)
63
    {
64
        $form = $this->buildNestedForm($this->column, $this->builder);
65
66
        $prepare = $form->prepare($input);
67
68
        return collect($prepare)->reject(function ($item) {
69
            return $item[NestedForm::REMOVE_FLAG_NAME] == 1;
70
        })->map(function ($item) {
71
            unset($item[NestedForm::REMOVE_FLAG_NAME]);
72
            return $item;
73
        })->toArray();
74
    }
75
76
    protected function getKeyName()
77
    {
78
        if (is_null($this->form)) {
79
            return;
80
        }
81
82
        return 'id';
83
    }
84
85 View Code Duplication
    protected function buildNestedForm($column, \Closure $builder, $key = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $form = new NestedForm($column);
88
89
        $form->setForm($this->form)
90
            ->setKey($key);
91
92
        call_user_func($builder, $form);
93
94
        $form->hidden(NestedForm::REMOVE_FLAG_NAME)->default(0)->addElementClass(NestedForm::REMOVE_FLAG_CLASS);
95
96
        return $form;
97
    }
98
99
    public function render()
100
    {
101
        return $this->renderTable();
102
    }
103
}