PHP code example of fratily / path-parser
1. Go to this page and download the library: Download fratily/path-parser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fratily / path-parser example snippets
$segments = \Fratily\PathParser\PathParser::parse('/foo/:id/bar/', [
\Fratily\PathParser\Segments\SlashSegment::class,
CustomSegment::class,
\Fratily\PathParser\Segments\PlainSegment::class,
]);
var_dump(
// /foo
get_class($segments[0]), // "Fratily\PathParser\Segments\PlainSegment"
$segments[0]->getSegment(), // "/foo"
// /:id
get_class($segments[1]), // "CustomSegment"
$segments[0]->getName(), // "/id"
// /bar
get_class($segments[2]), // "Fratily\PathParser\Segments\PlainSegment"
$segments[0]->getSegment(), // "/bar"
// /
get_class($segments[3]), // "Fratily\PathParser\Segments\SlashSegment"
);
class CustomSegment implements \Fratily\PathParser\Segments\SegmentInterface
{
private string $name;
public static function new(string $plainSegment): CustomSegment|null
{
if (1 !== preg_match('/\A:([a-z]+)\z/i', $plainSegment, $m)) {
return null;
}
$obj = new CustomSegment();
$obj->name = $m[1];
return $obj;
}
public static function getName(): string
{
return $this->name;
}
}