-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace expression assert with custom validator (#282)
This will: - title
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters