Url shortening demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

41 lines
1.1 KiB

<?php
declare(strict_types=1);
namespace App\Validator;
use App\Hasher;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
final class CustomStringNeverCollidesValidator extends ConstraintValidator
{
public function __construct(private Hasher $hasher)
{
}
/**
* @inheritDoc
*/
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof CustomStringNeverCollides) {
throw new UnexpectedTypeException($constraint, CustomStringNeverCollides::class);
}
if (null === $value || '' === $value) {
return;
}
if (!is_string($value)) {
throw new UnexpectedValueException($value, 'string');
}
/** Can be decoded to id, so avoid it */
if ($this->hasher->isValid($value)) {
$this->context->buildViolation($constraint->message)->addViolation();
}
}
}