Skip to content

UriTemplate extraction #1

Open
Open
@nyamsprod

Description

@nyamsprod

Feature Request

Q A
New Feature yes
BC Break no

Proposal

Adding the ability to extract variables from an URI using a UriTemplate object.

Proposed public API

use League\Uri\UriTemplate;
use League\Uri\UriTemplate\VariableBag;

$template = 'https://example.com/hotels/{hotel}/bookings/{booking}';
$uriString = 'https://example.com/hotels/Rest%20%26%20Relax/bookings/42';

$uriTemplate = new UriTemplate($template);
$variables = $uriTemplate->extract($uriString); 

// the extract method SHOULD accept as input
// - URI implementing objects (league and PSR-7) 
// - stringable objects
// - string

// the extract method MUST return a VariableBag object
$variables->isEmpty();      // return false
count($variables);          // returns 2
echo $variables['booking']; // returns '42'
echo $variables['hotel];    // returns 'Rest & Relax'

Good to know

  • The VariableBag object already exists
  • The internal representation of the UriTemplate broke down into expression and var specifier exists

Todo (implementation)

Create from an League\Uri\UriTemplate\Template object the correct regular expression for each expression and var specifier and process the URI input to get all existing variables.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions