MultiSelect::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Sco\Admin\Traits\HasSelectOptions;
7
8
/**
9
 * Class MultiSelect
10
 *
11
 * @package Sco\Admin\Form\Elements
12
 * @see http://element.eleme.io/#/en-US/component/select
13
 */
14
class MultiSelect extends NamedElement
15
{
16
    use HasSelectOptions;
17
18
    protected $type = 'select';
19
20
    protected $cast = 'json';
21
22
    protected $defaultValue = [];
23
24
    /**
25
     *
26
     * @param string $name
27
     * @param string $title
28
     * @param array|Model $options
29
     */
30
    public function __construct(string $name, string $title, $options = null)
31
    {
32
        parent::__construct($name, $title);
33
34
        if (! is_null($options)) {
35
            $this->setOptions($options);
36
        }
37
    }
38
39
    public function getValue()
40
    {
41
        $value = parent::getValue();
42
43
        if ($this->isOptionsModel() && $this->isRelation()) {
44
            $model = $this->getOptionsModel();
45
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
46
47
            return collect($value)->pluck($key)->map(function ($item) {
48
                return (string) $item;
49
            });
50
        }
51
52
        return (array) $value;
53
    }
54
55
    public function save()
56
    {
57
        if (! ($this->isOptionsModel() && $this->isRelation())) {
58
            return parent::save();
59
        }
60
    }
61
62 View Code Duplication
    public function finishSave()
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...
63
    {
64
        if (! ($this->isOptionsModel() && $this->isRelation())) {
65
            return;
66
        }
67
        $attribute = $this->getName();
68
        $values = $this->getValueFromRequest();
69
70
        $relation = $this->getModel()->{$attribute}();
71
        if ($relation instanceof BelongsToMany) {
72
            $relation->sync($values);
0 ignored issues
show
Bug introduced by
It seems like $values defined by $this->getValueFromRequest() on line 68 can also be of type string; however, Illuminate\Database\Eloq...sWithPivotTable::sync() does only seem to accept object<Illuminate\Databa...pport\Collection>|array, 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...
73
        }
74
    }
75
76
    protected function isRelation()
77
    {
78
        return method_exists($this->getModel(), $this->getName());
79
    }
80
81
    public function toArray()
82
    {
83
        return parent::toArray() + [
84
                'options'  => $this->getOptions(),
85
                'multiple' => true,
86
            ];
87
    }
88
}
89