Skip to content

Conversation

@nyamsprod
Copy link
Member

@kocsismate, @TimWolla since it would mean another wall of text I choose to create this POC instead. in response to https://news-web.php.net/php.internals/129595

TL;DR:

  • We introduce a UriPathType Enum
  • We introduce a UriPath class or UriPathSegments class (Naming is not that important for now)

The UriPathSegments has 2 properties:

UriPathType (absolute or relatives) and the segments.

Unless I am mistaking or forgetting something both URL and URI encode and decode path the same way. So in this case having 2 class is meaningless.

Segments could be accessed and manipulated decoded (which makes for improved DX) and the path is reconstructed safely using both properties (we stay away from how C# does its representation).

The class could be improved with more methods but for now I added

  • toRawString
  • toString (here the remove dot segments algorithm is used)

Of note in the case of the Url class there can not be any difference between both string representation but it can for the path coming from an RFC3986 URI class.

IMHO this version improve DX and avoid adding with* and get* methods on the URI/Url class because not all Url/Uri uses segments a good example being the URI/Url with the data scheme.

@nyamsprod nyamsprod self-assigned this Dec 12, 2025
@nyamsprod nyamsprod marked this pull request as draft December 12, 2025 15:37
@nyamsprod
Copy link
Member Author

nyamsprod commented Dec 12, 2025

For reference I use the following script to test the output

use Uri\Rfc3986\Uri;
use Uri\UriPathSegments;

$uriLists = [
    "https://example.com",
    "https://example.com/",
    "https://example.com/foo",
    "https://example.com/foo/",
    "foo/",
    "foo",
    "/foo",
    "/",
    '/a/b/c/./../../g',
];

foreach ($uriLists as $uri) {
    $uriObject = new Uri($uri);
    $pathSegments = UriPathSegments::fromUri($uriObject);
    dump([
        'uri' => $uri,
        'uri raw path' => $uriObject->getRawPath(),
        'uri path' => $uriObject->getPath(),
        'path type' => $pathSegments->getType(),
        'path segments' => $pathSegments->getAll(),
        'path raw string' => $pathSegments->toRawString(),
        'path string' => $pathSegments->toString(),
    ]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants