Skip to content

Commit

Permalink
Fixed README.md sample usages
Browse files Browse the repository at this point in the history
  • Loading branch information
brzuchal committed Feb 29, 2016
1 parent 2174d4b commit 53c8a71
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 18 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ At this point this library depends on another own library [madkom/collection](ht

1. [rize/uri-template](https://packagist.org/packages/rize/uri-template): for UriTemplate impl
2. [true/punycode](https://packagist.org/packages/true/punycode): for IDNA domain names conversion
3. [ml/iri](https://packagist.org/packages/ml/iri): for parsing URI _(this will be replaced with own impl because od to many dependencies and not as much RFC compliant impl)_

## Usage

Parsing url string:

```php
use Madkom\Uri\Parser;
use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;

$parser = new Parser();
$factory = new UriFactory();

/** @var Uri $uri */
$uri = $parser->parse('http://user:[email protected]/some/path?and=query&param=2#fragment');
$uri = $factory->createUri('http://user:[email protected]/some/path?and=query&param=2#fragment');

$uri->getScheme(); // Instance of \Madkom\Uri\Scheme\Http
$uri->getAuthority(); // Instance of \Madkom\Uri\Authority
Expand All @@ -56,13 +55,13 @@ $uri->getQuery(); // Instance of \Madkom\Uri\Query
Parsing isbn uri:

```php
use Madkom\Uri\Parser;
use Madkom\Uri\UriFactory;
use Madkom\Uri\Uri;

$parser = new Parser();
$factory = new UriFactory();

/** @var Uri $uri */
$uri = $parser->parse('isbn:978-83-283-0525-0'); // Instance of \Madkom\Uri\Uri
$uri = $factory->createUri('isbn:978-83-283-0525-0'); // Instance of \Madkom\Uri\Uri

$uri->getScheme(); // Instance of \Madkom\Uri\Scheme\Custom
$uri->getAuthority(); // NULL
Expand Down Expand Up @@ -106,9 +105,9 @@ $uri->toString(); // https://user:pass@[::1]:443/some/path?name=value
## TODO

* [ ] Implement Uri to string conversion
* [ ] Implement fragment component
* [ ] Replace IRI library with RFC Regex in `\Madkom\Uri\Parser`
* [ ] Implement additional parsing modes in `\Madkom\Uri\Parser\Query` for various languages _(parameter duplicate problem)_
* [x] Implement fragment component
* [x] Replace IRI library with RFC Regex in `\Madkom\Uri\Parser`
* [x] Implement additional parsing modes in `\Madkom\Uri\Parser\Query` for various languages _(parameter duplicate problem)_
* [ ] Implement normalization
* [ ] Implement UriReference based on *RFC3986*

Expand Down
47 changes: 47 additions & 0 deletions src/Scheme/Isbn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: mbrzuchalski
* Date: 29.02.16
* Time: 11:47
*/
namespace Madkom\Uri\Scheme;

use Madkom\Uri\Component\Authority;
use Madkom\Uri\Component\Fragment;
use Madkom\Uri\Component\Path;
use Madkom\Uri\Component\Query;
use Madkom\Uri\Uri;

/**
* Class Isbn
* @package Madkom\Uri\Scheme
* @author Michał Brzuchalski <[email protected]>
*/
class Isbn implements Scheme
{
const PROTOCOL = 'isbn';
/**
* Compose uri from parsed components
* @param Authority $authority
* @param Path $path
* @param Query $query
* @param Fragment $fragment
* @return Uri
*/
public function compose(Authority $authority, Path $path, Query $query, Fragment $fragment) : Uri
{
return new Uri($this, null, $path);
}

/**
* Retrieve uri string representation
* @param Uri $uri
* @param int $flags
* @return string
*/
public function toString(Uri $uri, int $flags = 0) : string
{
return '';
}
}
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Uri
* @param Query $query
* @param Fragment $fragment
*/
public function __construct(Scheme $scheme, Authority $authority, Path $path, Query $query = null, Fragment $fragment = null)
public function __construct(Scheme $scheme, Authority $authority = null, Path $path = null, Query $query = null, Fragment $fragment = null)
{
$this->scheme = $scheme;
$this->authority = $authority;
Expand Down
20 changes: 14 additions & 6 deletions src/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Madkom\Uri\Exception\ParseUriException;
use Madkom\Uri\Scheme\Http;
use Madkom\Uri\Scheme\Https;
use Madkom\Uri\Scheme\Isbn;
use Madkom\Uri\Scheme\Scheme;
use UnexpectedValueException;

Expand Down Expand Up @@ -101,6 +102,7 @@ class UriFactory
protected static $schemes = [
Http::PROTOCOL => Http::class,
Https::PROTOCOL => Https::class,
Isbn::PROTOCOL => Isbn::class,
];

/**
Expand All @@ -127,17 +129,23 @@ public function createUri(string $uriString, Scheme $defaultScheme = null) : Uri
}
}
if (null === $scheme) {
throw new MissingSchemeParseUriException("Malformed uri string given, missing scheme in: {$uriString}");
throw new MissingSchemeParseUriException("Malformed uri string, invalid scheme given: {$uriString}");
}
if (array_key_exists('authority', $matches) && $matches['authority']) {
$authority = $this->parseAuthority($matches['authority']);
}
$authority = $this->parseAuthority($matches['authority']);
$path = $this->parsePath($matches['path']);
$query = $this->parseQuery($matches['query'], $this->mode);
$fragment = new Fragment($matches['fragment']);
if (array_key_exists('query', $matches) && $matches['query']) {
$query = $this->parseQuery($matches['query'], $this->mode);
}
if (array_key_exists('fragment', $matches) && $matches['fragment']) {
$fragment = new Fragment($matches['fragment']);
}

return new Uri($scheme, $authority, $path, $query, $fragment);
return new Uri($scheme, $authority ?? null, $path, $query ?? null, $fragment ?? null);
}

throw new ParseUriException("Malformed uri string, unabel to parse, given: {$uriString}");
throw new ParseUriException("Malformed uri string, unable to parse, given: {$uriString}");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/UriFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ function it_can_parse_valid_url()

function it_fails_on_scheme_missing()
{
$this->shouldThrow(MissingSchemeParseUriException::class)->during('create', ['//user:[email protected]/path/to']);
$this->shouldThrow(MissingSchemeParseUriException::class)->during('createUri', ['//user:[email protected]/path/to']);
}
}

0 comments on commit 53c8a71

Please sign in to comment.