@@ -309,8 +309,9 @@ public function importContestData(mixed $data, ?string &$errorMessage = null, st
309
309
* problems?: array{name?: string, short-name?: string, id?: string, label?: string,
310
310
* letter?: string, label?: string, letter?: string}} $problems
311
311
* @param string[]|null $ids
312
+ * @param array<string, string[]> $messages
312
313
*/
313
- public function importProblemsData (Contest $ contest , array $ problems , array &$ ids = null ): bool
314
+ public function importProblemsData (Contest $ contest , array $ problems , array &$ ids = null , ? array & $ messages = [] ): bool
314
315
{
315
316
// For problemset.yaml the root key is called `problems`, so handle that case
316
317
// TODO: Move this check away to make the $problems array shape easier
@@ -329,8 +330,20 @@ public function importProblemsData(Contest $contest, array $problems, array &$id
329
330
->setTimelimit ($ problemData ['time_limit ' ] ?? 10 )
330
331
->setExternalid ($ problemData ['id ' ] ?? $ problemData ['short-name ' ] ?? $ problemLabel ?? null );
331
332
332
- $ this ->em ->persist ($ problem );
333
- $ this ->em ->flush ();
333
+ $ hasErrors = false ;
334
+ $ errors = $ this ->validator ->validate ($ problem );
335
+ if ($ errors ->count ()) {
336
+ $ hasErrors = true ;
337
+ /** @var ConstraintViolationInterface $error */
338
+ foreach ($ errors as $ error ) {
339
+ $ messages ['danger ' ][] = sprintf (
340
+ 'Error: problems.%s.%s: %s ' ,
341
+ $ problem ->getExternalid (),
342
+ $ error ->getPropertyPath (),
343
+ $ error ->getMessage ()
344
+ );
345
+ }
346
+ }
334
347
335
348
$ contestProblem = new ContestProblem ();
336
349
$ contestProblem
@@ -339,14 +352,35 @@ public function importProblemsData(Contest $contest, array $problems, array &$id
339
352
// We need to set both the entities and the IDs because of the composite primary key.
340
353
->setProblem ($ problem )
341
354
->setContest ($ contest );
355
+
356
+ $ errors = $ this ->validator ->validate ($ contestProblem );
357
+ if ($ errors ->count ()) {
358
+ $ hasErrors = true ;
359
+ /** @var ConstraintViolationInterface $error */
360
+ foreach ($ errors as $ error ) {
361
+ $ messages ['danger ' ][] = sprintf (
362
+ 'Error: problems.%s.contestproblem.%s: %s ' ,
363
+ $ problem ->getExternalid (),
364
+ $ error ->getPropertyPath (),
365
+ $ error ->getMessage ()
366
+ );
367
+ }
368
+ }
369
+
370
+ if ($ hasErrors ) {
371
+ return false ;
372
+
373
+ }
374
+
375
+ $ this ->em ->persist ($ problem );
342
376
$ this ->em ->persist ($ contestProblem );
377
+ $ this ->em ->flush ();
343
378
344
379
$ ids [] = $ problem ->getApiId ($ this ->eventLogService );
345
380
}
346
381
347
382
$ this ->em ->flush ();
348
383
349
- // For now this method will never fail so always return true.
350
384
return true ;
351
385
}
352
386
0 commit comments