for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Gacela\Router\Validators;
use Gacela\Router\Entities\RouteParams;
final class PathValidator
{
public static function isValid(string $path): bool
if ($path === '/') {
return true;
}
if ($path === '') {
return false;
if (str_starts_with($path, '/')) {
if (str_ends_with($path, '/')) {
$parts = explode('/', $path);
$optionalParamFound = false;
foreach ($parts as $part) {
if (!$part) { // Empty part found
} elseif (preg_match(RouteParams::OPTIONAL_PARAM_PATTERN, $part)) { // Optional argument found
$optionalParamFound = true;
} elseif ($optionalParamFound) { // Mandatory argument or static part found after an optional argument