Feat: Refactor profile data validation

This commit is contained in:
Björn Fromme
2023-09-20 10:06:39 +02:00
parent 7f7a35e7a2
commit 2dd8a6de19
10 changed files with 76 additions and 179 deletions
+27 -2
View File
@@ -2,13 +2,12 @@
namespace App\Entity\Embeddable; namespace App\Entity\Embeddable;
use App\Validator\Constraints as AppAssert;
use App\BusProNet\Model\ProfileResponse; use App\BusProNet\Model\ProfileResponse;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Embeddable] #[ORM\Embeddable]
#[AppAssert\Address(message: 'Bitte vervollständige deine Anschrift', groups: ['profile_preflight'])]
class Address class Address
{ {
#[ORM\Column(type: 'string', nullable: true)] #[ORM\Column(type: 'string', nullable: true)]
@@ -27,6 +26,32 @@ class Address
#[Assert\NotBlank(message: 'Bitte gib deinen Land an', groups: ['profile'])] #[Assert\NotBlank(message: 'Bitte gib deinen Land an', groups: ['profile'])]
protected ?string $country = null; protected ?string $country = null;
#[Assert\Callback(groups: ['profile_preflight'])]
public function preflightCheck(ExecutionContextInterface $executionContext): void
{
$missing = [];
if (empty($this->getStreet())) {
$missing[] = 'Straße';
}
if (empty($this->getCity())) {
$missing[] = 'Ort';
}
if (empty($this->getPostCode())) {
$missing[] = 'PLZ';
}
if (empty($this->getCountry())) {
$missing[] = 'Land';
}
if (0 < count($missing)) {
$executionContext
->buildViolation(sprintf('Bitte vervollständige deine Anschrift (%s).', implode(', ', $missing)))
->addViolation()
;
}
}
public function toPayload(): array public function toPayload(): array
{ {
return [ return [
+28 -2
View File
@@ -2,12 +2,11 @@
namespace App\Entity\Embeddable; namespace App\Entity\Embeddable;
use App\Validator\Constraints as AppAssert;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Embeddable] #[ORM\Embeddable]
#[AppAssert\BankAccount(message: 'Bitte vervollständige deine Bankverbindung', groups: ['profile_preflight'])]
class BankAccount class BankAccount
{ {
#[ORM\Column(nullable: true)] #[ORM\Column(nullable: true)]
@@ -27,6 +26,33 @@ class BankAccount
#[Assert\NotBlank(message: 'Bitte gib den Kontoinhaber an', groups: ['profile'])] #[Assert\NotBlank(message: 'Bitte gib den Kontoinhaber an', groups: ['profile'])]
private ?string $holder = null; private ?string $holder = null;
#[Assert\Callback(groups: ['profile_preflight'])]
public function preflightCheck(ExecutionContextInterface $executionContext): void
{
$missing = [];
if (empty($this->getIban())) {
$missing[] = 'IBAN';
}
if (empty($this->getBic())) {
$missing[] = 'BIC';
}
if (empty($this->getBank())) {
$missing[] = 'Name der Bank';
}
if (empty($this->getHolder())) {
$missing[] = 'Kontoinhaber';
}
if (0 < count($missing)) {
$executionContext
->buildViolation(sprintf('Bitte vervollständige deine Bankverbindung (%s)', implode(', ', $missing)))
->addViolation()
;
}
}
public function getIban(bool $obfuscated = false): ?string public function getIban(bool $obfuscated = false): ?string
{ {
if (false === $obfuscated) { if (false === $obfuscated) {
+21 -2
View File
@@ -2,13 +2,12 @@
namespace App\Entity\Embeddable; namespace App\Entity\Embeddable;
use App\Validator\Constraints as AppAssert;
use App\BusProNet\Model\ProfileResponse; use App\BusProNet\Model\ProfileResponse;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Embeddable] #[ORM\Embeddable]
#[AppAssert\Communication(message: 'Bitte vervollständige deine Kontaktdaten', groups: ['profile_preflight'])]
class Communication class Communication
{ {
#[ORM\Column(type: 'string', nullable: true)] #[ORM\Column(type: 'string', nullable: true)]
@@ -23,6 +22,26 @@ class Communication
#[Assert\NotBlank(message: 'Bitte gib deine E-Mail-Adresse an', groups: ['profile'])] #[Assert\NotBlank(message: 'Bitte gib deine E-Mail-Adresse an', groups: ['profile'])]
protected ?string $email = null; protected ?string $email = null;
#[Assert\Callback(groups: ['profile_preflight'])]
public function preflightCheck(ExecutionContextInterface $executionContext): void
{
$missing = [];
if (empty($this->getEmail())) {
$missing[] = 'E-Mail';
}
if (empty($this->getMobile())) {
$missing[] = 'Mobilnummer';
}
if (0 < count($missing)) {
$executionContext
->buildViolation(sprintf('Bitte vervollständige deine Kontaktdaten (%s)', implode(', ', $missing)))
->addViolation()
;
}
}
public function toPayload(): array public function toPayload(): array
{ {
return [ return [
-23
View File
@@ -1,23 +0,0 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class Address extends Constraint
{
public string $message = 'Die Anschrift ist unvollständig';
public function __construct(string $message = null, array $groups = null, $payload = null)
{
parent::__construct([], $groups, $payload);
$this->message = $message ?? $this->message;
}
public function getTargets(): string
{
return Constraint::CLASS_CONSTRAINT;
}
}
@@ -1,35 +0,0 @@
<?php
namespace App\Validator\Constraints;
use App\Entity\Embeddable\Address as AddressEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class AddressValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$value instanceof AddressEntity) {
throw new UnexpectedTypeException($value, AddressEntity::class);
}
if (!$constraint instanceof Address) {
throw new UnexpectedTypeException($constraint, Address::class);
}
$isIncomplete = null === $value->getStreet()
|| null === $value->getCity()
|| null === $value->getPostCode()
|| null === $value->getCountry()
;
if ($isIncomplete) {
$this->context
->buildViolation($constraint->message)
->addViolation()
;
}
}
}
-23
View File
@@ -1,23 +0,0 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class BankAccount extends Constraint
{
public string $message = 'Die Bankverbindung ist unvollständig';
public function __construct(string $message = null, array $groups = null, $payload = null)
{
parent::__construct([], $groups, $payload);
$this->message = $message ?? $this->message;
}
public function getTargets(): string
{
return Constraint::CLASS_CONSTRAINT;
}
}
@@ -1,35 +0,0 @@
<?php
namespace App\Validator\Constraints;
use App\Entity\Embeddable\BankAccount as BankAccountEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class BankAccountValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$value instanceof BankAccountEntity) {
throw new UnexpectedTypeException($value, BankAccountEntity::class);
}
if (!$constraint instanceof BankAccount) {
throw new UnexpectedTypeException($constraint, BankAccount::class);
}
$isIncomplete = null === $value->getIban()
|| null === $value->getBic()
|| null === $value->getBank()
|| null === $value->getHolder()
;
if ($isIncomplete) {
$this->context
->buildViolation($constraint->message)
->addViolation()
;
}
}
}
@@ -1,23 +0,0 @@
<?php
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class Communication extends Constraint
{
public string $message = 'Die Kontaktdaten sind unvollständig';
public function __construct(string $message = null, array $groups = null, $payload = null)
{
parent::__construct([], $groups, $payload);
$this->message = $message ?? $this->message;
}
public function getTargets(): string
{
return Constraint::CLASS_CONSTRAINT;
}
}
@@ -1,31 +0,0 @@
<?php
namespace App\Validator\Constraints;
use App\Entity\Embeddable\Communication as CommunicationEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class CommunicationValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$value instanceof CommunicationEntity) {
throw new UnexpectedTypeException($value, CommunicationEntity::class);
}
if (!$constraint instanceof Communication) {
throw new UnexpectedTypeException($constraint, Communication::class);
}
$isIncomplete = null === $value->getEmail() || null === $value->getMobile();
if ($isIncomplete) {
$this->context
->buildViolation($constraint->message)
->addViolation()
;
}
}
}
-3
View File
@@ -73,7 +73,6 @@
Anschrift Anschrift
</h3> </h3>
<div class="flex flex-col space-y-4 pb-8"> <div class="flex flex-col space-y-4 pb-8">
{{ form_errors(form.address) }}
{{ form_row(form.address.street) }} {{ form_row(form.address.street) }}
{{ form_row(form.address.postCode) }} {{ form_row(form.address.postCode) }}
{{ form_row(form.address.city) }} {{ form_row(form.address.city) }}
@@ -83,7 +82,6 @@
Kontakt Kontakt
</h3> </h3>
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
{{ form_errors(form.communication) }}
{{ form_row(form.communication.email) }} {{ form_row(form.communication.email) }}
{{ form_row(form.communication.phone) }} {{ form_row(form.communication.phone) }}
{{ form_row(form.communication.mobile) }} {{ form_row(form.communication.mobile) }}
@@ -95,7 +93,6 @@
Bankverbindung Bankverbindung
</h3> </h3>
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
{{ form_errors(form.bankAccount) }}
{{ form_row(form.bankAccount.iban) }} {{ form_row(form.bankAccount.iban) }}
{{ form_row(form.bankAccount.bic) }} {{ form_row(form.bankAccount.bic) }}
{{ form_row(form.bankAccount.bank) }} {{ form_row(form.bankAccount.bank) }}