|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Obblm\Core\Form\Team; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Obblm\Core\Contracts\RuleHelperInterface; |
|
7
|
|
|
use Obblm\Core\Entity\TeamVersion; |
|
8
|
|
|
use Obblm\Core\Validator\Constraints\Team\AdditionalSkills; |
|
9
|
|
|
use Symfony\Component\Form\AbstractType; |
|
10
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
|
11
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
|
13
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
14
|
|
|
use Symfony\Component\Form\FormInterface; |
|
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
16
|
|
|
|
|
17
|
|
|
class CompositionSkillsForm extends AbstractType implements DataMapperInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var RuleHelperInterface $helper */ |
|
22
|
|
|
$helper = $options['helper']; |
|
23
|
|
|
|
|
24
|
|
|
if ($builder->getData()) { |
|
25
|
|
|
/** @var TeamVersion $version */ |
|
26
|
|
|
$version = $builder->getData(); |
|
27
|
|
|
if ($version->getTeam()->getCreationOption('skills_allowed')) { |
|
28
|
|
|
$builder->add('playerVersions', CollectionType::class, [ |
|
29
|
|
|
'entry_type' => AddSkills::class, |
|
30
|
|
|
'allow_add' => false, |
|
31
|
|
|
'allow_delete' => false, |
|
32
|
|
|
'mapped' => true, |
|
33
|
|
|
'entry_options' => [ |
|
34
|
|
|
'helper' => $helper |
|
35
|
|
|
] |
|
36
|
|
|
]); |
|
37
|
|
|
$builder->setDataMapper($this); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function mapDataToForms($viewData, iterable $forms) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($viewData === null) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
if (!$viewData instanceof TeamVersion) { |
|
49
|
|
|
throw new UnexpectedTypeException($viewData, TeamVersion::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @var FormInterface[] $forms */ |
|
53
|
|
|
$forms = iterator_to_array($forms); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$players = $viewData->getNotDeadPlayerVersions(); |
|
56
|
|
|
$onlyPositionals = new ArrayCollection(); |
|
57
|
|
|
foreach ($players as $player) { |
|
58
|
|
|
if (!$player->getPlayer()->isStarPlayer()) { |
|
59
|
|
|
$onlyPositionals->add($player); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
$forms['playerVersions']->setData($onlyPositionals); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
public function mapFormsToData(iterable $forms, &$viewData) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($viewData === null) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
if (!$viewData instanceof TeamVersion) { |
|
72
|
|
|
throw new UnexpectedTypeException($viewData, TeamVersion::class); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
77
|
|
|
{ |
|
78
|
|
|
$resolver->setDefaults([ |
|
79
|
|
|
'translation_domain' => 'obblm', |
|
80
|
|
|
'data_class' => TeamVersion::class, |
|
81
|
|
|
'helper' => null, |
|
82
|
|
|
'constraints' => [ |
|
83
|
|
|
new AdditionalSkills() |
|
84
|
|
|
] |
|
85
|
|
|
]); |
|
86
|
|
|
$resolver->setAllowedTypes('helper', [RuleHelperInterface::class]); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|