Passed
Pull Request — 1.x (#35)
by Marko
05:54
created

FieldCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace KunicMarko\SonataAnnotationBundle\Grouping;
7
8
9
use Sonata\AdminBundle\Form\FormMapper;
10
11
/**
12
 * @author Marko Kunic <[email protected]>
13
 */
14
final class FieldCollection
15
{
16
    use SortableElementsTrait;
17
18
    /**
19
     * @var Field[]
20
     */
21
    private $fields = [];
22
23
    private function __construct()
24
    {
25
    }
26
27
    public static function create(): self
28
    {
29
        return new self();
30
    }
31
32
    public function add(Field $field): void
33
    {
34
        if (isset($this->fields[$field->getName()])) {
35
            throw new \InvalidArgumentException(sprintf(
36
                'Field "%s" already in collection.',
37
                $field->getName()
38
            ));
39
        }
40
41
        $this->fields[$field->getName()] = $field;
42
    }
43
44
    public function isEmpty(): bool
45
    {
46
        return \count($this->fields) === 0;
47
    }
48
49
    public function render(FormMapper $formMapper): void
50
    {
51
        $this->fields = $this->sort(...\array_values($this->fields));
52
53
        foreach ($this->fields as $field) {
54
            $formMapper->add($field->getName(), ...$field->getSettings());
0 ignored issues
show
Bug introduced by
$field->getSettings() is expanded, but the parameter $type of Sonata\AdminBundle\Form\FormMapper::add() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            $formMapper->add($field->getName(), /** @scrutinizer ignore-type */ ...$field->getSettings());
Loading history...
55
        }
56
    }
57
}