BelongsToMany::onEdit()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 20
rs 9.7998
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
class BelongsToMany extends HasMany
6
{
7
    const MODE_TAGS = 'tags';
8
    const MODE_CHECKBOXES = 'checkboxes';
9
10
    /** @var string */
11
    protected $icon = 'random';
12
13
    /** @var string */
14
    protected $titleField = 'name';
15
16
    /** @var string */
17
    protected $editMode = self::MODE_CHECKBOXES;
18
19
    /** @var bool */
20
    protected $completeList = true;
21
22
    /**
23
     * Show editable controls as checkboxes.
24
     *
25
     * @return self
26
     */
27
    public function tagList(): self
28
    {
29
        $this->editMode = static::MODE_TAGS;
30
        $this->completeList = false;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param  string  $column
37
     * @return BelongsToMany
38
     */
39
    public function useAsTitle(string $column): self
40
    {
41
        $this->titleField = $column;
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function onEdit(): array
50
    {
51
        $relation = $this->relation();
52
53
        if (static::MODE_CHECKBOXES === $this->editMode && $this->completeList) {
54
            $values = $this->query
55
                ? call_user_func_array($this->query, [$relation->getRelated()->query()])
56
                : $relation->getRelated()->all();
57
        } else {
58
            $values = $this->value();
59
        }
60
61
        return [
62
            'relation' => $relation,
63
            'searchable' => \get_class($relation->getRelated()),
64
            'values' => $values,
65
            'completeList' => $this->completeList,
66
            'titleField' => $this->titleField,
67
            'editMode' => $this->editMode,
68
        ];
69
    }
70
}
71