I made a URL parsing/building package, looking for feedback

You can find the package - https://github.com/KepplerPl/url

And documentation for it - https://kepplerpl-scheme.readthedocs.io/en/latest/

All in all it’s not too complicated and intended to help dealing with urls and maintain the rfc standard to some degree.

Since it’s fairly big, I’ll just leave the readme found on github here.

Simple package to manipulate URLs

Read the docs documentation

Installation

composer require keppler/url

Quickstart

Parser and Builder

This package is split into 2 independent pieces. These pieces are also split into several other pieces.

The Scheme.php will be referred to as Parser in the documentation.

Parser

The Parser is the entry point for parsing a url. It’s immutable, meaning you cannot change it once it is created.

The scheme is split into schemes such as ftp, http, https, mailto, etc.
Each scheme is used to parse a single url type, as you might have guessed.

require 'vendor/autoload.php';

$url =  'ftp://user:password@host:123/path';

$scheme = Scheme::ftp($url);

print_r($scheme->all());

...

Array
(
    [scheme] => ftp
    [user] => user
    [password] => password
    [host] => host
    [port] => 123
    [path] => Array
        (
            [0] => path
        )

)

At the time of this writing the parser supports 4 schemes: FTP, HTTPS, HTTP, and MAILTO

Builder

The Builder.php class is the entry point for modifying a url or simply creating one from scratch.
If you choose to build from an existing url you must pass it a Parser instance with the appropriate scheme.

At the time of this writing the Builder supports 4 schemes: FTP, HTTPS, HTTP, and MAILTO

require 'vendor/autoload.php';

$url =  'ftp://user:password@host:123/path';

$ftpScheme = Scheme::ftp($url);
$builder = Builder::ftp($ftpScheme);

$builder->setHost('example.com')
    ->setPassword('hunter2')
    ->setPort(5);

print_r($builder->raw());
...
ftp://user:[email protected]:5/path/

print_r($builder->encoded());
...
ftp://user:[email protected]:5/path/to+encode/ // notice the extra +

Both the Parser and the Builder can be used independently.

Each supported scheme can also be used independently without the Builder or the Parser. Examples bellow.

Independent usage

Assuming you don’t want to use the Parser/Builder classes directly you can choose not to.

Each scheme supported can be used independently of the Parser/Builder.

$ftpUrl = 'ftp://user:password@host:123/path';

$ftpImmutable = new FtpImmutable($ftpUrl);

echo $ftpImmutable->raw();

$ftpBuilder = new FtpBuilder();

$ftpBuilder->setHost('host')
    ->setPassword('hunter2')
    ->setPort(987)
    ->setUser('hunter');

$ftpBuilder->getPathBag()
    ->set(0, 'path')
    ->set(1, 'new path');

echo $ftpBuilder->raw(); // ftp://hunter:hunter2@host:987/path/new path/

echo $ftpBuilder->encoded(); // ftp://hunter:hunter2@host:987/path/new+path/
Sponsor our Newsletter | Privacy Policy | Terms of Service