Skip to content

Instantly share code, notes, and snippets.

@Astikos
Created February 21, 2023 06:10
Show Gist options
  • Save Astikos/3ad05c81270e714b6158dcbe5611818c to your computer and use it in GitHub Desktop.
Save Astikos/3ad05c81270e714b6158dcbe5611818c to your computer and use it in GitHub Desktop.
<?php
namespace App\Entity;
use App\Exception\InvalidRegularExpression;
class UrlPattern
{
private ?int $id;
private string $regex;
/** @throws InvalidRegularExpression */
public function __construct(?int $id, string $regex)
{
$this->id = $id;
if (false === preg_match($regex, '')) {
throw new InvalidRegularExpression();
}
$this->regex = $regex;
}
public function getId(): ?int
{
return $this->id;
}
public function returnNonNullId(): int
{
if (null === $this->id) {
throw new \LogicException(sprintf('id should not be null for UrlPattern %s', $this->regex));
}
return $this->id;
}
public function getRegex(): string
{
return $this->regex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment