feat: refactor callback validators to dedicated class
This commit is contained in:
@@ -5,9 +5,10 @@ namespace App\Form\Model;
|
||||
use App\BusProNet\Model\PersonalData;
|
||||
use App\BusProNet\Model\Pickup;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Validator\Constraints as AppAssert;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
#[AppAssert\Participant]
|
||||
class ParticipantData
|
||||
{
|
||||
public ?int $index = null;
|
||||
@@ -44,74 +45,6 @@ class ParticipantData
|
||||
public ?Service $transportationServiceFro = null;
|
||||
public ?Pickup $pickup = null;
|
||||
|
||||
#[Assert\Callback]
|
||||
public function assertBodyMeasurementsValid(ExecutionContextInterface $context): void
|
||||
{
|
||||
if (0 === count($this->rentals)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->height)) {
|
||||
$context->buildViolation('Bitte angeben wegen Leihmaterial')
|
||||
->atPath('height')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
|
||||
if (empty($this->shoeSize)) {
|
||||
$context->buildViolation('Bitte angeben wegen Leihmaterial')
|
||||
->atPath('shoeSize')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
|
||||
if (empty($this->weight)) {
|
||||
$context->buildViolation('Bitte angeben wegen Leihmaterial')
|
||||
->atPath('weight')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
#[Assert\Callback]
|
||||
public function assertTransportationSelected(ExecutionContextInterface $context): void
|
||||
{
|
||||
if ('S' === $this->status) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $this->transportationServiceTo) {
|
||||
$context->buildViolation('Bitte angeben')
|
||||
->atPath('transportationServiceTo')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
|
||||
if (null === $this->transportationServiceFro) {
|
||||
$context->buildViolation('Bitte angeben')
|
||||
->atPath('transportationServiceFro')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
#[Assert\Callback]
|
||||
public function assertPickupSelected(ExecutionContextInterface $context): void
|
||||
{
|
||||
$transportationServiceTo = $this->transportationServiceTo;
|
||||
|
||||
if (
|
||||
null !== $transportationServiceTo
|
||||
&& 'BUS' === $transportationServiceTo->subType
|
||||
&& null === $this->pickup
|
||||
) {
|
||||
$context->buildViolation('Bitte auswählen')
|
||||
->atPath('pickup')
|
||||
->addViolation()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromPersonalData(PersonalData $personalData): static
|
||||
{
|
||||
$instance = new static();
|
||||
@@ -133,4 +66,14 @@ class ParticipantData
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function isCanceled(): bool
|
||||
{
|
||||
return 'S' === $this->status;
|
||||
}
|
||||
|
||||
public function isOption(): bool
|
||||
{
|
||||
return 'O' === $this->status;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user