|
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 GroupCollection |
|
15
|
|
|
{ |
|
16
|
|
|
use SortableElementsTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Group[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $groups = []; |
|
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(Group $group): void |
|
33
|
|
|
{ |
|
34
|
|
|
if (isset($this->groups[$group->getName()])) { |
|
35
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
36
|
|
|
'Group "%s" already in collection.', |
|
37
|
|
|
$group->getName() |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->groups[$group->getName()] = $group; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Group[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function all(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->groups; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function has(string $name): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return isset($this->groups[$name]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function get(string $name): Group |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->has($name)) { |
|
60
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
61
|
|
|
'Group "%s" not found in collection.', |
|
62
|
|
|
$name |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->groups[$name]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function isEmpty(): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return \count($this->groups) === 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function render(FormMapper $formMapper): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->groups = $this->sort(...\array_values($this->groups)); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($this->groups as $group) { |
|
79
|
|
|
$formMapper->with($group->getName()); |
|
80
|
|
|
|
|
81
|
|
|
$group->getFieldCollection()->render($formMapper); |
|
82
|
|
|
|
|
83
|
|
|
$formMapper->end(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |