Giter VIP home page Giter VIP logo

phpuri's Introduction

phpUri

Set of classes to parse and validate URI.

Basic Usage

The easiest way to start is to create a StringBasedUriFactory instance with injected validator and parser implementations and call createUri method:

<?php
use EPS\PhpUri\Factory\StringBasedUriFactory;
use EPS\PhpUri\Parser\Rfc3986Parser;
use EPS\PhpUri\Validator\Rfc3986Validator;

$uriFactory = new StringBasedUriFactory(
    new Rfc3986Validator(),
    new Rfc3986Parser()
);
$uri = $uriFactory->createUri('http://user:[email protected]/path');

The result of createUri method is an Uri instance with URI parts:

  • Scheme
  • Authority
    • User
    • Password
    • Host
    • Port
  • Path
  • Query
  • Fragment

Validation

Input URI string can be validate by one of validators implementing Validator interface. The Validator::validate($uri) method can return true or throw an ValidatorException if validation fails. Currently following validators are available:

RFC Validators

  • Rfc2396Validator - based on RFC 2396 specs. Internally uses filter_var function with FILTER_VALIDATE_URL filter which is compatible with this spec.
  • Rfc3986Validator - based on RFC 3986 specs. Uses set of regular expressions to strictly match specs conditions.

Parts validators

You can validate only part of an URI with one (or more) validators available. Each URI part has own validator which can be used explicitly:

  • SchemeValidator
  • AuthorityValidator
  • PathValidator
  • QueryValidator
  • FragmentValidator

Each of these validators follow RFC 3986 specs.

Aggregated validators

Very probably that you may want to use many validators in validation process. The AggregatedValidator class can run validation one-by-one by calling validate method (from Validator interface):

<?php

$aggregated = new \EPS\PhpUri\Validator\AggregatedValidator([
    new \EPS\PhpUri\Validator\SchemeValidator(),
    new \EPS\PhpUri\Validator\QueryValidator()
]);
$result = $aggregated->validate('http://example.com?query=123');

Note: Actually, the Rfc3986Validator encapsulates an AggregatedVaidator with SchemeValidator, AuthorityValidator, PathValidator, QueryValidator and FragmentValidator

Parsing

Parsers implement Parser interface and returns an Uri object. Currently there are two implementations available:

Formatting

An UriFormatter implementing Formatter interface will return encoded URI from URI object. This process is also RFC 3986 compatible.

REST API

This project contains very simple REST API built on Silex. You can validate and parse your URI via HTTP by sending a json with URI OR send uri parts to create URI.

Routes implemented so far:

  • GET /uri - send an URI in json to retrieve URI parts. Response codes:
    • 200 - successful validation and parsing
    • 400 - improperly formatted request
    • 422 - validation failed
    • 500 - generic error
  • POST /uri - send URI parts to retrieve json with formatted URI
    • 201 - URI created
    • 400 - improperly formatted request
    • 500 - generic error

The response of GET /uri and request payload of POST /uri is the same - it's serialized URI object.

Running on PHP server

You can use PHP built-in HTTP server to run this REST API. Simply run:

php -S localhost:8081 -t web/

Examples

# Parse URI
curl http://localhost:8018/uri -XGET -d '{"uri":"http:\/\/example.com\/some\/path"}' -H 'Content-Type: application/json'

# Create URI
curl http://localhost:8081/uri -XPOST -d '{"scheme":"http","authority":{"host":"example.com"}}' -H 'Content-Type: application/json' 

Development and testing

To install project, first you need to download of dependencies using Composer:

composer install

To run unit test with PHPUnit, use following command:

bin/phpunit 

phpuri's People

Contributors

eps90 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.