Skip to content

Commit

Permalink
Replace expression assert with custom validator (#282)
Browse files Browse the repository at this point in the history
This will:
- title
  • Loading branch information
jskowronski39 authored Aug 18, 2023
2 parents 91527cf + 2f4f1f3 commit 99e4d22
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Form/Mod/Dto/ModFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Entity\Mod\Enum\ModSourceEnum;
use App\Form\AbstractFormDto;
use App\Validator\Mod\ModSourceAndType;
use App\Validator\Mod\SteamWorkshopArma3ModUrl;
use App\Validator\Mod\UniqueDirectoryMod;
use App\Validator\Mod\UniqueSteamWorkshopMod;
Expand All @@ -16,6 +17,7 @@
#[UniqueSteamWorkshopMod(groups: [ModSourceEnum::STEAM_WORKSHOP])]
#[SteamWorkshopArma3ModUrl(groups: [ModSourceEnum::STEAM_WORKSHOP], errorPath: 'url', nameErrorPath: 'name')]
#[UniqueDirectoryMod(groups: [ModSourceEnum::DIRECTORY])]
#[ModSourceAndType(errorPath: 'source')]
class ModFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;
Expand All @@ -27,12 +29,10 @@ class ModFormDto extends AbstractFormDto
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

#[Assert\Expression("!(this.getType() != constant('App\\Entity\\Mod\\Enum\\ModTypeEnum::SERVER_SIDE') && this.getSource() == constant('App\\Entity\\Mod\\Enum\\ModSourceEnum::DIRECTORY'))")]
protected ?string $type = null;

protected ?string $status = null;

#[Assert\Expression("!(this.getSource() == constant('App\\Entity\\Mod\\Enum\\ModSourceEnum::DIRECTORY') && this.getType() != constant('App\\Entity\\Mod\\Enum\\ModTypeEnum::SERVER_SIDE'))")]
protected ?string $source = null;

#[Assert\NotBlank(groups: [ModSourceEnum::STEAM_WORKSHOP])]
Expand Down
32 changes: 32 additions & 0 deletions src/Validator/Mod/ModSourceAndType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Validator\Mod;

use Symfony\Component\Validator\Constraint;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class ModSourceAndType extends Constraint
{
public string $message = 'Mod from a directory must be a server side mod';
public ?string $errorPath = null;

public function __construct(
string $message = null,
string $errorPath = null,
$options = null,
array $groups = null,
$payload = null
) {
parent::__construct($options, $groups, $payload);

$this->message = $message ?? $this->message;
$this->errorPath = $errorPath ?? $this->errorPath;
}

public function getTargets(): array|string
{
return parent::CLASS_CONSTRAINT;
}
}
32 changes: 32 additions & 0 deletions src/Validator/Mod/ModSourceAndTypeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Validator\Mod;

use App\Entity\Mod\Enum\ModSourceEnum;
use App\Entity\Mod\Enum\ModTypeEnum;
use App\Form\Mod\Dto\ModFormDto;
use App\Validator\AbstractValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class ModSourceAndTypeValidator extends AbstractValidator
{
public function validate($value, Constraint $constraint): void
{
if (!$value instanceof ModFormDto) {
throw new UnexpectedTypeException($constraint, ModFormDto::class);
}

if (!$constraint instanceof ModSourceAndType) {
throw new UnexpectedTypeException($constraint, ModSourceAndType::class);
}

if (ModSourceEnum::DIRECTORY === $value->getSource() && ModTypeEnum::SERVER_SIDE === $value->getType()) {
return;
}

$this->addViolation($constraint->message, [], $constraint->errorPath);
}
}
3 changes: 3 additions & 0 deletions translations/validators.pl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ Mod group with the same name "{{ modGroupName }}" already exist: Grupa modów o

# Unique User Group Name Validator
User group with the same name "{{ userGroupName }}" already exist: Grupa użytkowników o nazwie "{{ userGroupName }}" już istnieje

# Mod Source And Type Validator
Mod from a directory must be a server side mod: Mod pochodzący z katalogu musi być serwerowy

0 comments on commit 99e4d22

Please sign in to comment.