Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Replace ParserException by Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 8, 2016
1 parent a7fcd7d commit 0e782b5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ All Notable changes to `league-uri-parser` will be documented in this file

### Added

- `Parser::isValidHost` method
- `ParserException::createFromInvalidScheme` replaces `ParserException::createFromInvalidState` usage
- `ParserException::createFromInvalidPath` replaces `ParserException::createFromInvalidState` usage
- `League\Uri\Exception` replaces `League\Uri\ParserException`
- `League\Uri\Parser::isValidHost` method
- `League\Uri\Exception::createFromInvalidScheme` replaces `ParserException::createFromInvalidState` usage
- `League\Uri\Exception::createFromInvalidPath` replaces `ParserException::createFromInvalidState` usage

### Fixed

Expand All @@ -20,14 +21,15 @@ All Notable changes to `league-uri-parser` will be documented in this file

### Removed

- `ParserException::createFromInvalidState`
- `League\Uri\ParserException` replaced by `Exception`
- `League\Uri\ParserException::createFromInvalidState`

## 0.2.0 - 2016-11-02

### Added

- `ParserException` class which extends SPL `InvalidArgumentException`
- `HostValidation` trait to ease Host validation without the parser
- `League\Uri\ParserException` class which extends SPL `InvalidArgumentException`
- `League\Uri\HostValidation` trait to ease Host validation without the parser

### Fixed

Expand Down
22 changes: 11 additions & 11 deletions src/ParserException.php → src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,65 @@
* @author Ignace Nyamagana Butera <[email protected]>
* @since 0.2.0
*/
class ParserException extends InvalidArgumentException
class Exception extends InvalidArgumentException
{
/**
* Returns a new Instance from an error in URI characters
*
* @param string $uri
*
* @return self
* @return static
*/
public static function createFromInvalidCharacters($uri)
{
return new self(sprintf('The submitted uri `%s` contains invalid characters', $uri));
return new static(sprintf('The submitted uri `%s` contains invalid characters', $uri));
}

/**
* Returns a new Instance from an error in URI characters
*
* @param string $uri
*
* @return self
* @return static
*/
public static function createFromInvalidScheme($uri)
{
return new self(sprintf('The submitted uri `%s` contains an invalid scheme', $uri));
return new static(sprintf('The submitted uri `%s` contains an invalid scheme', $uri));
}

/**
* Returns a new Instance from an error in Host validation
*
* @param string $host
*
* @return self
* @return static
*/
public static function createFromInvalidHost($host)
{
return new self(sprintf('The submitted host `%s` is invalid', $host));
return new static(sprintf('The submitted host `%s` is invalid', $host));
}

/**
* Returns a new Instance from an error in port validation
*
* @param string $port
*
* @return self
* @return static
*/
public static function createFromInvalidPort($port)
{
return new self(sprintf('The submitted port `%s` is invalid', $port));
return new static(sprintf('The submitted port `%s` is invalid', $port));
}

/**
* Returns a new Instance from an error in Uri path component
*
* @param string $uri
*
* @return self
* @return static
*/
public static function createFromInvalidPath($uri)
{
return new self(sprintf('The submitted uri `%s` contains an invalid path', $uri));
return new static(sprintf('The submitted uri `%s` contains an invalid path', $uri));
}
}
4 changes: 2 additions & 2 deletions src/HostValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait HostValidation
*
* @param string $host
*
* @throws ParserException If the host component is invalid
* @throws Exception If the host component is invalid
*
* @return string
*/
Expand All @@ -41,7 +41,7 @@ protected function filterHost($host)
return $host;
}

throw ParserException::createFromInvalidHost($host);
throw Exception::createFromInvalidHost($host);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class Parser
*
* @param string $uri
*
* @throws ParserException if the URI contains invalid characters
* @throws Exception if the URI contains invalid characters
*
* @return array
*/
Expand All @@ -103,7 +103,7 @@ public function __invoke($uri)
}

if (strlen($uri) !== strcspn($uri, self::INVALID_URI_CHARS)) {
throw ParserException::createFromInvalidCharacters($uri);
throw Exception::createFromInvalidCharacters($uri);
}

//if the first characters is a known URI delimiter parsing
Expand Down Expand Up @@ -162,7 +162,7 @@ public function __invoke($uri)
*
* @param string $uri
*
* @throws ParserException If the port is invalid
* @throws Exception If the port is invalid
*
* @return array
*/
Expand Down Expand Up @@ -229,7 +229,7 @@ private function parseUriWithoutScheme($uri)
}

if (':' !== $hostname[$port_delimiter_index]) {
throw ParserException::createFromInvalidPort(substr($hostname, $port_delimiter_index));
throw Exception::createFromInvalidPort(substr($hostname, $port_delimiter_index));
}

$final['port'] = $this->filterPort(substr($hostname, $port_delimiter_index + 1));
Expand Down Expand Up @@ -263,7 +263,7 @@ private function parseUriWithoutScheme($uri)
*
* @param string $uri
*
* @throws ParserException If the path component is invalid
* @throws Exception If the path component is invalid
*
* @return array
*/
Expand All @@ -272,7 +272,7 @@ private function parseUriWithoutSchemeAndAuthority($uri)
//No scheme is present so we ensure that if presence of a path-noscheme
//RFC3986 is respected
if (false !== ($pos = strpos($uri, ':')) && false === strpos(substr($uri, 0, $pos), '/')) {
throw ParserException::createFromInvalidPath($uri);
throw Exception::createFromInvalidPath($uri);
}

//Parsing is done from the right upmost part to the left
Expand All @@ -297,7 +297,7 @@ private function parseUriWithoutSchemeAndAuthority($uri)
*
* @param mixed $port the port number
*
* @throws ParserException If the port number is invalid.
* @throws Exception If the port number is invalid.
*
* @return null|int
*/
Expand All @@ -316,7 +316,7 @@ private function filterPort($port)
return $formatted_port;
}

throw ParserException::createFromInvalidPort($port);
throw Exception::createFromInvalidPort($port);
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ private function filterPort($port)
*
* @param string $uri
*
* @throws ParserException If the URI scheme component is empty
* @throws Exception If the URI scheme component is empty
*
* @return array
*/
Expand All @@ -358,7 +358,7 @@ private function parseUriWithColonCharacter($uri)

//1.1 - a scheme can not be empty (ie a URI can not start with a colon)
if ('' === $scheme) {
throw ParserException::createFromInvalidScheme($uri);
throw Exception::createFromInvalidScheme($uri);
}

//2 - depending on the scheme presence and validity we will differ the
Expand Down
4 changes: 2 additions & 2 deletions test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LeagueTest\Uri;

use League\Uri\Exception;
use League\Uri\Parser;
use League\Uri\ParserException;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -590,7 +590,7 @@ public function testValidURI()
*/
public function testParseFailed($uri)
{
$this->setExpectedException(ParserException::class);
$this->setExpectedException(Exception::class);
$this->parser->__invoke($uri);
}

Expand Down

0 comments on commit 0e782b5

Please sign in to comment.