mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Added validation to not found redirects for domain
This commit is contained in:
@@ -8,13 +8,26 @@ use JsonSerializable;
|
||||
|
||||
final class NotFoundRedirects implements JsonSerializable
|
||||
{
|
||||
public function __construct(
|
||||
private ?string $baseUrlRedirect = null,
|
||||
private ?string $regular404Redirect = null,
|
||||
private ?string $invalidShortUrlRedirect = null,
|
||||
private function __construct(
|
||||
private ?string $baseUrlRedirect,
|
||||
private ?string $regular404Redirect,
|
||||
private ?string $invalidShortUrlRedirect,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function withRedirects(
|
||||
?string $baseUrlRedirect = null,
|
||||
?string $regular404Redirect = null,
|
||||
?string $invalidShortUrlRedirect = null,
|
||||
): self {
|
||||
return new self($baseUrlRedirect, $regular404Redirect, $invalidShortUrlRedirect);
|
||||
}
|
||||
|
||||
public static function withoutRedirects(): self
|
||||
{
|
||||
return new self(null, null, null);
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrlRedirect;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Domain\Validation;
|
||||
|
||||
use Laminas\InputFilter\InputFilter;
|
||||
use Laminas\Validator;
|
||||
use Shlinkio\Shlink\Common\Validation;
|
||||
|
||||
class DomainRedirectsInputFilter extends InputFilter
|
||||
{
|
||||
use Validation\InputFactoryTrait;
|
||||
|
||||
public const DOMAIN = 'domain';
|
||||
public const BASE_URL_REDIRECT = 'baseUrlRedirect';
|
||||
public const REGULAR_404_REDIRECT = 'regular404Redirect';
|
||||
public const INVALID_SHORT_URL_REDIRECT = 'invalidShortUrlRedirect';
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function withData(array $data): self
|
||||
{
|
||||
$instance = new self();
|
||||
|
||||
$instance->initializeInputs();
|
||||
$instance->setData($data);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function initializeInputs(): void
|
||||
{
|
||||
$domain = $this->createInput(self::DOMAIN);
|
||||
$domain->getValidatorChain()->attach(new Validator\NotEmpty([
|
||||
Validator\NotEmpty::OBJECT,
|
||||
Validator\NotEmpty::SPACE,
|
||||
Validator\NotEmpty::NULL,
|
||||
Validator\NotEmpty::EMPTY_ARRAY,
|
||||
Validator\NotEmpty::BOOLEAN,
|
||||
]));
|
||||
$this->add($domain);
|
||||
|
||||
$this->add($this->createInput(self::BASE_URL_REDIRECT, false));
|
||||
$this->add($this->createInput(self::REGULAR_404_REDIRECT, false));
|
||||
$this->add($this->createInput(self::INVALID_SHORT_URL_REDIRECT, false));
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->persist($detachedDomain);
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$detachedWithRedirects->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com'));
|
||||
$this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
@@ -101,7 +101,7 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||
$this->getEntityManager()->persist($detachedDomain);
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$detachedWithRedirects->configureNotFoundRedirects(NotFoundRedirects::withRedirects('foo.com', 'bar.com'));
|
||||
$this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
@@ -183,7 +183,7 @@ class DomainServiceTest extends TestCase
|
||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
||||
$flush = $this->em->flush();
|
||||
|
||||
$result = $this->domainService->configureNotFoundRedirects($authority, new NotFoundRedirects(
|
||||
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
||||
'foo.com',
|
||||
'bar.com',
|
||||
'baz.com',
|
||||
|
||||
Reference in New Issue
Block a user