1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace KunicMarko\SonataAnnotationBundle\Grouping; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Marko Kunic <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
final class Group implements SortableElement |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $options = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Tab|null |
24
|
|
|
*/ |
25
|
|
|
private $tab; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $tabName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int|null |
34
|
|
|
*/ |
35
|
|
|
private $position; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FieldCollection |
39
|
|
|
*/ |
40
|
|
|
private $fieldCollection; |
41
|
|
|
|
42
|
|
|
private function __construct(string $name, array $options = [], ?string $tabName = null, ?int $position = null) |
43
|
|
|
{ |
44
|
|
|
$this->name = $name; |
45
|
|
|
$this->options = $options; |
46
|
|
|
$this->tabName = $tabName; |
47
|
|
|
$this->position = $position; |
48
|
|
|
$this->fieldCollection = FieldCollection::create(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function with(string $name, array $options = [], ?string $tabName = null, ?int $position = null): self |
52
|
|
|
{ |
53
|
|
|
return new self($name, $options, $tabName, $position); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setTab(Tab $tab): void |
57
|
|
|
{ |
58
|
|
|
if ($tab->getName() !== $this->tabName) { |
59
|
|
|
throw new \InvalidArgumentException(sprintf( |
60
|
|
|
'Group "%s" has "%s" as a Tab, trying to add it to "%s" Tab failed.', |
61
|
|
|
$this->name, |
62
|
|
|
$this->tabName, |
63
|
|
|
$tab->getName() |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->tab !== null) { |
68
|
|
|
throw new \InvalidArgumentException(sprintf( |
69
|
|
|
'Group "%s" already has "%s" as a Tab, unable to add group to "%s" Tab.', |
70
|
|
|
$this->name, |
71
|
|
|
$this->tab->getName(), |
72
|
|
|
$tab->getName() |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->tab = $tab; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getTabName(): ?string |
80
|
|
|
{ |
81
|
|
|
return $this->tabName; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getName(): string |
85
|
|
|
{ |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function addField(Field $field): void |
90
|
|
|
{ |
91
|
|
|
$field->setGroup($this); |
92
|
|
|
$this->fieldCollection->add($field); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getPosition(): ?int |
96
|
|
|
{ |
97
|
|
|
return $this->position; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getFieldCollection(): FieldCollection |
101
|
|
|
{ |
102
|
|
|
return $this->fieldCollection; |
103
|
|
|
} |
104
|
|
|
} |