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

TabCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 21
dl 0
loc 62
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 3 1
A add() 0 10 2
A create() 0 3 1
A __construct() 0 2 1
A render() 0 10 2
A has() 0 3 1
A get() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Grouping;
6
7
use Sonata\AdminBundle\Form\FormMapper;
8
9
/**
10
 * @author Marko Kunic <[email protected]>
11
 */
12
final class TabCollection
13
{
14
    use SortableElementsTrait;
15
16
    /**
17
     * @var Tab[]
18
     */
19
    private $tabs = [];
20
21
    private function __construct()
22
    {
23
    }
24
25
    public static function create(): self
26
    {
27
        return new self();
28
    }
29
30
    public function add(Tab $tab): void
31
    {
32
        if ($this->has($tab->getName())) {
33
            throw new \InvalidArgumentException(sprintf(
34
                'Tab "%s" already in collection.',
35
                $tab->getName()
36
            ));
37
        }
38
39
        $this->tabs[$tab->getName()] = $tab;
40
    }
41
42
    public function has(string $name): bool
43
    {
44
        return isset($this->tabs[$name]);
45
    }
46
47
    public function get(string $name): Tab
48
    {
49
        if (!$this->has($name)) {
50
            throw new \InvalidArgumentException(sprintf(
51
                'Tab "%s" not found in collection.',
52
                $name
53
            ));
54
        }
55
56
        return $this->tabs[$name];
57
    }
58
59
    public function isEmpty(): bool
60
    {
61
        return \count($this->tabs) === 0;
62
    }
63
64
    public function render(FormMapper $formMapper): void
65
    {
66
        $this->tabs = $this->sort(...\array_values($this->tabs));
67
68
        foreach ($this->tabs as $tab) {
69
            $formMapper->tab($tab->getName());
70
71
            $tab->getGroupCollection()->render($formMapper);
72
73
            $formMapper->end();
74
        }
75
    }
76
}