-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: Extract a ConstraintErrorsFormatter from BaseController
This formatter is now reusable outside of controllers, making it more useful!
- Loading branch information
1 parent
8f5ce4a
commit cc94181
Showing
15 changed files
with
70 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
// This file is part of Bileto. | ||
// Copyright 2022-2023 Probesys | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
namespace App\Utils; | ||
|
||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
class ConstraintErrorsFormatter | ||
{ | ||
/** | ||
* Format a list of errors returned by a Validator into a list of errors | ||
* displayable in the interface. | ||
* | ||
* The keys are the properties of the concerned Entity, and the values are | ||
* a concatenation of the corresponding error messages. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
public static function format(ConstraintViolationListInterface $errors): array | ||
{ | ||
$formattedErrors = []; | ||
foreach ($errors as $error) { | ||
$property = $error->getPropertyPath(); | ||
if (isset($formattedErrors[$property])) { | ||
$formattedErrors[$property] = implode( | ||
' ', | ||
[$formattedErrors[$property], $error->getMessage()], | ||
); | ||
} else { | ||
$formattedErrors[$property] = $error->getMessage(); | ||
} | ||
} | ||
return $formattedErrors; | ||
} | ||
} |