fratily / path-parser
0.2.0
2021-11-20 13:25 UTC
Requires
- php: ^8.0
Requires (Dev)
- phpstan/phpstan: ^0.12.99
- phpstan/phpstan-phpunit: ^0.12.22
- phpstan/phpstan-strict-rules: ^0.12.11
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2025-02-20 20:31:11 UTC
README
fratily/path-parser
is parser of url pathname.
Install
$ composer require fratily/path-parser
Usage
$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; } }