feat: refactor callback validators to dedicated class

This commit is contained in:
Björn Fromme
2025-03-26 16:08:25 +01:00
parent e4e03a848b
commit 72d413dc99
4 changed files with 94 additions and 70 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ use Symfony\Component\Validator\Constraint;
#[\Attribute]
class Booking extends Constraint
{
public string $message = 'WTF?';
public string $message = 'Bitte prüfe deine Angaben.';
public function getTargets(): array|string
{
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class Participant extends Constraint
{
public function getTargets(): array|string
{
return static::CLASS_CONSTRAINT;
}
}
@@ -0,0 +1,67 @@
<?php
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantData;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ParticipantValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
/** @var ParticipantData $participant */
$participant = $value;
$this->assertBodyMeasurementsValid($participant);
$this->assertTransportationSelected($participant);
$this->assertPickupSelected($participant);
}
public function assertBodyMeasurementsValid(ParticipantData $participant): void
{
if (0 === count($participant->rentals)) {
return;
}
foreach (['height', 'shoeSize', 'weight'] as $property) {
if (empty($participant->{$property})) {
$this->context->buildViolation('Bitte angeben wegen Leihmaterial')
->atPath($property)
->addViolation()
;
}
}
}
public function assertTransportationSelected(ParticipantData $participant): void
{
// no transportation services required for canceled participants
if (true === $participant->isCanceled()) {
return;
}
foreach (['transportationServiceTo', 'transportationServiceFro'] as $property) {
if (null === $participant->{$property}) {
$this->context->buildViolation('Bitte angeben')
->atPath($property)
->addViolation()
;
}
}
}
public function assertPickupSelected(ParticipantData $participant): void
{
if (
null !== $participant->transportationServiceTo
&& 'BUS' === $participant->transportationServiceTo->subType
&& null === $participant->pickup
) {
$this->context->buildViolation('Bitte auswählen')
->atPath('pickup')
->addViolation()
;
}
}
}