Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Created August 11, 2023 07:36

Revisions

  1. nyamsprod created this gist Aug 11, 2023.
    7 changes: 7 additions & 0 deletions tools.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?php

    use League\Uri\Idna\Converter as IdnConverter;
    use League\Uri\IPv4\Converter as Ipv4Converter;

    echo IdnConverter::toAscii('bébé.be')->domain(), PHP_EOL; //display "xn--bb-bjab.be"
    echo Ipv4Converter::fromEnvironment()->toDecimal('192.87.125'); //display "192.87.0.125"
    15 changes: 15 additions & 0 deletions uri-new-api.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    <?php

    use League\Uri\BaseUri;
    use League\Uri\Modifier;

    $uri = "https://www.bbc.co.uk";

    $resolveUri = BaseUri::from($uri)
    ->resolve("path/to/../../the/sky/?foo.bar");

    $newUri = Modifier::from($resolveUri)
    ->appendQuery('foo.bar=tata')
    ->removeLabels(1)
    ->removeTrailingSlash()
    ->getUriString();
    18 changes: 18 additions & 0 deletions uri-old-api.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php

    use League\Uri\Uri;
    use League\Uri\UriModifier;
    use League\Uri\UriResolver;

    $uri = "https://www.bbc.co.uk";
    $relativeUri = "path/to/../../the/sky/?foo.bar";

    $baseUri = Uri::createFromString($uri);
    $relativeUri = Uri::createFromString($relativeUri);
    $resolveUri = UriResolver::resolve($relativeUri, $baseUri);
    $newUri = UriModifier::appendQuery($resolveUri, 'foo.bar=tata');
    $newUri = UriModifier::removeLabels($newUri, 1);
    $newUri = UriModifier::removeTrailingSlash($newUri);
    $newUri = $newUri->__toString();

    echo $newUri, PHP_EOL;
    19 changes: 19 additions & 0 deletions uri-stricter.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php

    use League\Uri\UriTemplate;
    $template = 'https://api.example.com/{version}/search/{term}/{?q*,limit}';

    $params = [
    'term' => ['john', 'doe'],
    'q' => ['a', 'b'],
    'limit' => '10',
    ];

    $uriTemplate = new UriTemplate($template);
    echo $uriTemplate->expand($params);
    // display https://api.example.com//search/john,doe/?q=a&q=b&limit=10 with missing version
    // this is valid and follow the specifications

    echo $uriTemplate->expandOrFail($params);
    // will throw a TemplateCanNotBeExpanded exception with the following message
    // Missing variables `version`