Issues (368)

Fields/Renderer/ObjectRelationGroupedRenderer.php (1 issue)

1
<?php
2
3
namespace Arbory\Base\Admin\Form\Fields\Renderer;
4
5
use Arbory\Base\Html\Html;
6
use Arbory\Base\Html\Elements\Element;
7
use Illuminate\Database\Eloquent\Model;
8
9
class ObjectRelationGroupedRenderer extends ObjectRelationRenderer
10
{
11
    /**
12
     * @return Element
13
     */
14
    public function render()
15
    {
16
        if ($this->field->hasIndentation()) {
17
            throw new \InvalidArgumentException('Field cannot be grouped and indented at the same time');
18
        }
19
20
        return parent::render()->addAttributes(['data-grouped' => $this->field->getGroupByAttribute()]);
21
    }
22
23
    /**
24
     * @return Element[]
25
     */
26
    protected function getAvailableRelationalItemsElement()
27
    {
28
        $items = [];
29
        $relationalGroups = $this->field->getOptions();
30
31
        foreach ($relationalGroups as $group) {
32
            foreach ($group as $relation) {
33
                $name = $this->getGroupName($relation);
34
35
                if (! array_key_exists($name, $items)) {
36
                    $items[$name] = Html::div(Html::strong($name)->addClass('title'))->addClass('group');
37
                }
38
39
                $element = $this->buildRelationalItemElement($relation, $this->field->hasRelationWith($relation));
40
41
                $items[$name]->append($element);
42
            }
43
        }
44
45
        return $items;
46
    }
47
48
    /**
49
     * @param  Model  $relation
50
     * @return string
51
     */
52
    private function getGroupName(Model $relation)
53
    {
54
        $attribute = $this->field->getGroupByAttribute();
55
        $getName = $this->field->getGroupByGetName();
56
57
        return $getName ? $getName($relation) : $relation->getAttribute($attribute);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $getName ? $getNa...etAttribute($attribute) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
58
    }
59
}
60