Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
Update Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 1, 2016
1 parent 5a9fd35 commit 740a258
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All Notable changes to `uri-manipulations` will be documented in this file

## Next
## 0.1.0 - 2016-12-01

### Added

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"require": {
"php" : ">=5.6",
"psr/http-message": "^1.0",
"league/uri-components": "dev-master",
"league/uri-components": "~0.4",
"league/uri-interfaces": "~0.2"
},
"require-dev": {
"league/uri-schemes": "dev-master",
"league/uri-schemes": "~0.3",
"guzzlehttp/psr7": "^1.2",
"league/uri-parser": "~0.2",
"friendsofphp/php-cs-fixer": "^1.9",
Expand Down Expand Up @@ -62,7 +62,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
"dev-master": "1.0.x-dev"
}
},
"config": {
Expand Down
18 changes: 13 additions & 5 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
*/
class Formatter
{
const RFC3986 = Component::RFC3986;
const RFC3986_ENCODING = Component::RFC3986_ENCODING;

const RFC3987 = Component::RFC3987;
const RFC3987_ENCODING = Component::RFC3987_ENCODING;

const NO_ENCODING = Component::NO_ENCODING;

/**
* host encoding property
*
* @var string
*/
protected $enc_type = self::RFC3986;
protected $enc_type = self::RFC3986_ENCODING;

/**
* query separator property
Expand Down Expand Up @@ -70,8 +72,14 @@ class Formatter
*/
public function setEncoding($enc_type)
{
if (!in_array($enc_type, [self::RFC3987, self::RFC3986])) {
throw new InvalidArgumentException('Unknown encoding rule');
$enc_type_list = [
Component::RFC3986_ENCODING => 1,
Component::RFC3987_ENCODING => 1,
Component::NO_ENCODING => 1,
];

if (!isset($enc_type_list[$enc_type])) {
throw new InvalidArgumentException(sprintf('Unsupported or Unknown Encoding: %s', $enc_type));
}

$this->enc_type = $enc_type;
Expand Down
2 changes: 1 addition & 1 deletion src/HostToAscii.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class HostToAscii extends ManipulateHost
*/
protected function modifyHost($str)
{
return (string) (new Host($str))->getContent(Host::RFC3986);
return (string) (new Host($str))->getContent(Host::RFC3986_ENCODING);
}
}
2 changes: 1 addition & 1 deletion src/HostToUnicode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class HostToUnicode extends ManipulateHost
*/
protected function modifyHost($str)
{
return (string) (new Host($str))->getContent(Host::RFC3987);
return (string) (new Host($str))->getContent(Host::RFC3987_ENCODING);
}
}
12 changes: 6 additions & 6 deletions test/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function setUp()

public function testFormatHostAscii()
{
$this->formatter->setEncoding(Formatter::RFC3986);
$this->formatter->setEncoding(Formatter::RFC3986_ENCODING);
$this->assertSame('xn--gwd-hna98db.pl', $this->formatter->__invoke(new Host('gwóźdź.pl')));
}

Expand All @@ -53,7 +53,7 @@ public function testFormatWithSimpleString()
$uri = Http::createFromString($uri);

$this->formatter->setQuerySeparator('&');
$this->formatter->setEncoding(Formatter::RFC3986);
$this->formatter->setEncoding(Formatter::RFC3986_ENCODING);
$this->assertSame($expected, $this->formatter->__invoke($uri));
}

Expand All @@ -72,7 +72,7 @@ public function testFormatComponent()

public function testFormatHostUnicode()
{
$this->formatter->setEncoding(Formatter::RFC3987);
$this->formatter->setEncoding(Formatter::RFC3987_ENCODING);
$this->assertSame('gwóźdź.pl', $this->formatter->__invoke(new Host('gwóźdź.pl')));
}

Expand All @@ -93,7 +93,7 @@ public function testFormatQueryWithSeparator()
public function testFormat()
{
$this->formatter->setQuerySeparator('&');
$this->formatter->setEncoding(Formatter::RFC3986);
$this->formatter->setEncoding(Formatter::RFC3986_ENCODING);
$expected = 'http://login:[email protected]:443/test/query.php?kingkong=toto&foo=bar+baz#doc3';
$this->assertSame($expected, $this->formatter->__invoke($this->uri));
}
Expand All @@ -102,7 +102,7 @@ public function testFormatOpaqueUri()
{
$uri = Data::createFromString('data:,');
$this->formatter->setQuerySeparator('&');
$this->formatter->setEncoding(Formatter::RFC3986);
$this->formatter->setEncoding(Formatter::RFC3986_ENCODING);
$this->assertSame($uri->__toString(), $this->formatter->__invoke($uri));
}

Expand All @@ -111,7 +111,7 @@ public function testFormatWithoutAuthority()
$expected = '/test/query.php?kingkong=toto&foo=bar+baz#doc3';
$uri = Http::createFromString('/test/query.php?kingkong=toto&foo=bar+baz#doc3');
$this->formatter->setQuerySeparator('&');
$this->formatter->setEncoding(Formatter::RFC3986);
$this->formatter->setEncoding(Formatter::RFC3986_ENCODING);
$this->assertSame($expected, $this->formatter->__invoke($uri));
}

Expand Down

0 comments on commit 740a258

Please sign in to comment.