Skip to content

Commit

Permalink
Refactor validators; replace annotations with attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jskowronski39 committed Aug 15, 2023
1 parent 734a58d commit 2bdc5ff
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Api\DataTransformer\Attendance;

use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\Validator\ValidatorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Api\Input\Attendance\AttendanceInput;
use App\Entity\Attendance\Attendance;
use Ramsey\Uuid\Uuid;
Expand Down
16 changes: 5 additions & 11 deletions src/Api/Input/Attendance/AttendanceInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@
use App\Validator\SteamProfileId;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @UniqueAttendance
*/
#[UniqueAttendance]
class AttendanceInput
{
/**
* @Assert\NotBlank
* @Assert\Length(max=255)
*/
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
protected ?string $missionId = null;

/**
* @Assert\NotBlank
* @SteamProfileId
*/
#[Assert\NotBlank]
#[SteamProfileId]
protected ?int $playerId = null;

public function getMissionId(): ?string
Expand Down
22 changes: 7 additions & 15 deletions src/Form/Dlc/Dto/DlcFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @SteamStoreArma3DlcUrl(errorPath="url")
* @UniqueSteamStoreDlc
* @UniqueDirectoryDlc
*/
#[SteamStoreArma3DlcUrl(errorPath: 'url')]
#[UniqueSteamStoreDlc]
#[UniqueDirectoryDlc]
class DlcFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @Assert\Length(max=255)
*/
#[Assert\Length(max: 255)]
protected ?string $name = null;

/**
* @Assert\Length(min=1, max=255)
*/
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

protected ?string $url = null;

/**
* @Assert\NotBlank
* @WindowsDirectoryName
*/
#[Assert\NotBlank]
#[WindowsDirectoryName]
protected ?string $directory = null;

public function getId(): ?UuidInterface
Expand Down
42 changes: 12 additions & 30 deletions src/Form/Mod/Dto/ModFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,34 @@
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @UniqueSteamWorkshopMod(groups={ModSourceEnum::STEAM_WORKSHOP})
* @SteamWorkshopArma3ModUrl(groups={ModSourceEnum::STEAM_WORKSHOP}, errorPath="url", nameErrorPath="name")
* @UniqueDirectoryMod(groups={ModSourceEnum::DIRECTORY})
*/
#[UniqueSteamWorkshopMod(groups: [ModSourceEnum::STEAM_WORKSHOP])]
#[SteamWorkshopArma3ModUrl(groups: [ModSourceEnum::STEAM_WORKSHOP], errorPath: 'url', nameErrorPath: 'name')]
#[UniqueDirectoryMod(groups: [ModSourceEnum::DIRECTORY])]
class ModFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @Assert\NotBlank(groups={ModSourceEnum::DIRECTORY})
* @Assert\Length(min=1, max=255)
*/
#[Assert\NotBlank(groups: [ModSourceEnum::DIRECTORY])]
#[Assert\Length(min: 1, max: 255)]
protected ?string $name = null;

/**
* @Assert\Length(min=1, max=255)
*/
#[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'))",
* )
*/
#[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'))",
* )
*/
#[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})
* @Assert\Length(min=1, max=255, groups={ModSourceEnum::STEAM_WORKSHOP})
*/
#[Assert\NotBlank(groups: [ModSourceEnum::STEAM_WORKSHOP])]
#[Assert\Length(min: 1, max: 255, groups: [ModSourceEnum::STEAM_WORKSHOP])]
protected ?string $url = null;

/**
* @Assert\NotBlank(groups={ModSourceEnum::DIRECTORY})
* @WindowsDirectoryName(groups={ModSourceEnum::DIRECTORY})
*/
#[Assert\NotBlank(groups: [ModSourceEnum::DIRECTORY])]
#[WindowsDirectoryName(groups: [ModSourceEnum::DIRECTORY])]
protected ?string $directory = null;

public function resolveValidationGroups(): array
Expand Down
14 changes: 4 additions & 10 deletions src/Form/ModGroup/Dto/ModGroupFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @UniqueModGroupName(errorPath="name")
*/
#[UniqueModGroupName(errorPath: 'name')]
class ModGroupFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @Assert\NotBlank
* @Assert\Length(max=255)
*/
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
protected ?string $name = null;

/**
* @Assert\Length(min=1, max=255)
*/
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

/**
Expand Down
10 changes: 3 additions & 7 deletions src/Form/ModList/Dto/ModListFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ class ModListFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @Assert\NotBlank
* @Assert\Length(max=255)
*/
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
protected ?string $name = null;

/**
* @Assert\Length(min=1, max=255)
*/
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Form/User/Dto/UserFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class UserFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @SteamProfileId
*/
#[SteamProfileId]
protected ?int $steamId = null;

protected ?UserPermissions $permissions = null;
Expand Down
14 changes: 4 additions & 10 deletions src/Form/UserGroup/Dto/UserGroupFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @UniqueUserGroupName(errorPath="name")
*/
#[UniqueUserGroupName(errorPath: 'name')]
class UserGroupFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;

/**
* @Assert\NotBlank
* @Assert\Length(max=255)
*/
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
protected ?string $name = null;

/**
* @Assert\Length(min=1, max=255)
*/
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

protected ?UserGroupPermissions $permissions = null;
Expand Down
17 changes: 14 additions & 3 deletions src/Validator/Attendance/UniqueAttendance.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class UniqueAttendance extends Constraint
{
public string $message = 'Attendance of player "{{ playerId }}" in mission "{{ missionId }}" already exists';
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;
Expand Down
21 changes: 18 additions & 3 deletions src/Validator/Dlc/SteamStoreArma3DlcUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,31 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class SteamStoreArma3DlcUrl extends Constraint
{
public string $invalidDlcUrlMessage = 'Invalid Steam Store DLC url';
public string $dlcNotFoundMessage = 'DLC not found';
public string $notAnArma3DlcMessage = 'Url is not an Arma 3 DLC';
public ?string $errorPath = null;

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

$this->invalidDlcUrlMessage = $invalidDlcUrlMessage ?? $this->{$invalidDlcUrlMessage};
$this->dlcNotFoundMessage = $dlcNotFoundMessage ?? $this->dlcNotFoundMessage;
$this->notAnArma3DlcMessage = $notAnArma3DlcMessage ?? $this->notAnArma3DlcMessage;
$this->errorPath = $errorPath ?? $this->errorPath;
}

public function getTargets(): array|string
{
return parent::CLASS_CONSTRAINT;
Expand Down
17 changes: 14 additions & 3 deletions src/Validator/Dlc/UniqueDirectoryDlc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class UniqueDirectoryDlc extends Constraint
{
public string $message = 'DLC associated with directory "{{ directoryName }}" already exist';
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;
Expand Down
17 changes: 14 additions & 3 deletions src/Validator/Dlc/UniqueSteamStoreDlc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class UniqueSteamStoreDlc extends Constraint
{
public string $message = 'DLC associated with url "{{ dlcUrl }}" already exist';
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;
Expand Down
25 changes: 22 additions & 3 deletions src/Validator/Mod/SteamWorkshopArma3ModUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class SteamWorkshopArma3ModUrl extends Constraint
{
public string $invalidModUrlMessage = 'Invalid Steam Workshop mod url';
Expand All @@ -18,6 +16,27 @@ class SteamWorkshopArma3ModUrl extends Constraint
public ?string $errorPath = null;
public ?string $nameErrorPath = null;

public function __construct(
string $invalidModUrlMessage = null,
string $modNotFoundMessage = null,
string $notAnArma3ModMessage = null,
string $modIsPrivateOrMissingDetails = null,
string $errorPath = null,
string $nameErrorPath = null,
$options = null,
array $groups = null,
$payload = null
) {
parent::__construct($options, $groups, $payload);

$this->invalidModUrlMessage = $invalidModUrlMessage ?? $this->invalidModUrlMessage;
$this->modNotFoundMessage = $modNotFoundMessage ?? $this->modNotFoundMessage;
$this->notAnArma3ModMessage = $notAnArma3ModMessage ?? $this->notAnArma3ModMessage;
$this->modIsPrivateOrMissingDetails = $modIsPrivateOrMissingDetails ?? $this->modIsPrivateOrMissingDetails;
$this->errorPath = $errorPath ?? $this->errorPath;
$this->nameErrorPath = $nameErrorPath ?? $this->nameErrorPath;
}

public function getTargets(): array|string
{
return parent::CLASS_CONSTRAINT;
Expand Down
Loading

0 comments on commit 2bdc5ff

Please sign in to comment.