← Back
Editing: AgencyImportException.php
<?php namespace App\Import; use Symfony\Component\Validator\ConstraintViolationInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; class AgencyImportException extends \Exception { public function __construct( string $className, ConstraintViolationListInterface $errors, int $position = -1, array $values = [] ) { $message = $this->handleValidationErrors($errors, $className, $position, $values); parent::__construct($message, 0, null); } private function handleValidationErrors( ConstraintViolationListInterface $errors, string $className, int $position, array $values ): string { $messages = []; foreach ($errors as $error) { $messages[] = $this->formatError($error, $className, $position, $values); } return implode('<br />', $messages); } private function formatError( ConstraintViolationInterface $error, string $className, int $position, array $values ): string { $context = self::getLabelFromModelName($className, $values); if ($position >= 0) { // The data starts at row 2 in the spreadsheet (row 1 is the header). $context .= ', ligne '.($position + 2); } $field = self::getFieldLabel($error->getPropertyPath()); $message = $context.' — champ « '.$field.' » : '.$error->getMessage(); $invalidValue = $error->getInvalidValue(); if (is_scalar($invalidValue) && '' !== (string) $invalidValue) { $message .= ' (valeur fournie : « '.$invalidValue.' »)'; } return $message; } private static function getFieldLabel(string $propertyPath): string { $labels = [ 'email' => 'adresse email', 'mailContact' => 'email du contact', 'nom' => 'nom', 'prenom' => 'prénom', 'nomContact' => 'nom du contact', 'prenomContact' => 'prénom du contact', 'telephoneFixe' => 'téléphone fixe', 'telephoneContact' => 'téléphone du contact', 'codePostal' => 'code postal', 'ville' => 'ville', 'voie' => 'voie', 'numero' => 'numéro', ]; return $labels[$propertyPath] ?? $propertyPath; } private static function getLabelFromModelName(string $className, array $values): string { // Users are all imported as User::class; the role tells us what kind of user it is. if ('App\Entity\User' === $className) { return self::getUserLabelFromRoles($values['roles'] ?? []); } $labels = [ 'App\Entity\Agency' => 'Import agence', 'App\Entity\Adresse' => 'Import adresse', 'App\Entity\Prestataire' => 'Import prestataire', 'App\Entity\AgencyPrestataire' => 'Import prestataire agence', ]; return $labels[$className] ?? $className; } private static function getUserLabelFromRoles(array $roles): string { $labels = [ 'ROLE_AGENT' => 'Import agent', 'ROLE_MANAGER' => 'Import manager', 'ROLE_SUPERVISOR' => 'Import superviseur', ]; foreach ($labels as $role => $label) { if (in_array($role, $roles, true)) { return $label; } } return 'Import utilisateur'; } }
Save File
Cancel