1 | <?php |
||
20 | class MatchFormCreator extends ModelFormCreator |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | 1 | protected function build($builder) |
|
26 | { |
||
27 | 1 | $durations = \Service::getParameter('bzion.league.duration'); |
|
28 | 1 | foreach ($durations as $duration => &$value) { |
|
29 | 1 | $durations[$duration] = $duration; |
|
30 | } |
||
31 | |||
32 | return $builder |
||
33 | 1 | ->add('first_team', new MatchTeamType(), array( |
|
34 | 1 | 'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
|
|||
35 | )) |
||
36 | 1 | ->add('second_team', new MatchTeamType(), array( |
|
37 | 1 | 'disableTeam' => $this->isEdit() && $this->editing->isOfficial() |
|
38 | )) |
||
39 | 1 | ->add('duration', 'choice', array( |
|
40 | 1 | 'choices' => $durations, |
|
41 | 1 | 'constraints' => new NotBlank(), |
|
42 | 'expanded' => true |
||
43 | )) |
||
44 | 1 | ->add('server_address', 'text', array( |
|
45 | 1 | 'required' => false, |
|
46 | 'attr' => array('placeholder' => 'brad.guleague.org:5100'), |
||
47 | )) |
||
48 | 1 | ->add('time', new DatetimeWithTimezoneType(), array( |
|
49 | 'constraints' => array( |
||
50 | 1 | new NotBlank(), |
|
51 | 1 | new LessThan(array( |
|
52 | 1 | 'value' => \TimeDate::now()->addMinutes(10), |
|
53 | 1 | 'message' => 'The timestamp of the match must not be in the future' |
|
54 | )) |
||
55 | ), |
||
56 | 1 | 'data' => ($this->isEdit()) |
|
57 | ? $this->editing->getTimestamp()->setTimezone(\Controller::getMe()->getTimezone()) |
||
58 | 1 | : \TimeDate::now(\Controller::getMe()->getTimezone()), |
|
59 | 1 | 'with_seconds' => $this->isEdit() |
|
60 | )) |
||
61 | 1 | ->add('map', new ModelType('Map'), array( |
|
62 | 'required' => false |
||
63 | 1 | )) |
|
64 | 1 | ->add('type', 'choice', array( |
|
65 | 'choices' => array( |
||
66 | \Match::OFFICIAL => 'Official', |
||
67 | \Match::FUN => 'Fun match', |
||
68 | \Match::SPECIAL => 'Special event match', |
||
69 | 1 | ), |
|
70 | 1 | 'disabled' => $this->editing && $this->editing->isOfficial(), |
|
71 | 1 | 'label' => 'Match Type' |
|
72 | )) |
||
73 | 1 | ->add('enter', 'submit'); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | * |
||
79 | * @param \Match $match |
||
80 | */ |
||
81 | public function fill($form, $match) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | * |
||
104 | * @param \Match $match |
||
105 | */ |
||
106 | public function update($form, $match) |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | public function enter($form) |
|
151 | { |
||
152 | 1 | $firstTeam = $form->get('first_team'); |
|
153 | 1 | $secondTeam = $form->get('second_team'); |
|
154 | |||
155 | 1 | $firstId = $firstTeam->get('team')->getData()->getId(); |
|
156 | 1 | $secondId = $secondTeam->get('team')->getData()->getId(); |
|
157 | |||
158 | 1 | $official = ($form->get('type')->getData() === \Match::OFFICIAL); |
|
159 | 1 | $serverInfo = $this->getServerInfo($form->get('server_address')); |
|
160 | |||
161 | 1 | $match = \Match::enterMatch( |
|
162 | 1 | $official ? $firstId : null, |
|
163 | 1 | $official ? $secondId : null, |
|
164 | 1 | $firstTeam->get('score')->getData(), |
|
165 | 1 | $secondTeam->get('score')->getData(), |
|
166 | 1 | $form->get('duration')->getData(), |
|
167 | 1 | $this->me->getId(), |
|
168 | 1 | $form->get('time')->getData(), |
|
169 | 1 | $this->getPlayerList($firstTeam), |
|
170 | 1 | $this->getPlayerList($secondTeam), |
|
171 | 1 | $serverInfo[0], |
|
172 | 1 | $serverInfo[1], |
|
173 | 1 | null, |
|
174 | 1 | $form->get('map')->getData()->getId(), |
|
175 | 1 | $form->get('type')->getData(), |
|
176 | 1 | $official ? null : $firstId, |
|
177 | 1 | $official ? null : $secondId |
|
178 | ); |
||
179 | |||
180 | 1 | return $match; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get the player list of a team |
||
185 | * |
||
186 | * @param FormInterface $team A MatchTeamType form |
||
187 | * @return array |
||
188 | */ |
||
189 | 1 | private function getPlayerList(FormInterface $team) |
|
193 | |||
194 | /** |
||
195 | * Get the server address and port of a match |
||
196 | * |
||
197 | * @param FormInterface $server A text form representing the server |
||
198 | * @return array |
||
199 | */ |
||
200 | 1 | private function getServerInfo(FormInterface $server) |
|
209 | |||
210 | /** |
||
211 | * Get a function which converts models to their IDs |
||
212 | * |
||
213 | * Useful to store the match players into the database |
||
214 | */ |
||
215 | private static function getModelToID() |
||
221 | } |
||
222 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: