Files
myep/src/Validator/Constraints/ParticipantValidator.php
T

71 lines
2.3 KiB
PHP

<?php
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantDto;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ParticipantValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
/** @var ParticipantDto $participant */
$participant = $value;
$this->assertBodyMeasurementsValid($participant);
$this->assertTransportationSelected($participant);
$this->assertPickupSelected($participant);
}
public function assertBodyMeasurementsValid(ParticipantDto $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(ParticipantDto $participant): void
{
// no transportation services required for canceled participants
if (true === $participant->isCanceled()) {
return;
}
foreach (['transportationOutbound', 'transportationInbound'] as $property) {
if (null === $participant->{$property}) {
$this->context->buildViolation('Bitte angeben')
->atPath($property)
->addViolation()
;
}
}
}
public function assertPickupSelected(ParticipantDto $participant): void
{
// Check if either outbound or inbound transportation is bus
$hasOutboundBus = null !== $participant->transportationOutbound
&& 'BUS' === $participant->transportationOutbound->subType;
$hasInboundBus = null !== $participant->transportationInbound
&& 'BUS' === $participant->transportationInbound->subType;
// Require pickup if at least one direction has bus transport
if (($hasOutboundBus || $hasInboundBus) && null === $participant->pickup) {
$this->context->buildViolation('Bitte auswählen')
->atPath('pickup')
->addViolation()
;
}
}
}