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

Commit

Permalink
adding toArray and bug fix Query
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 20, 2015
1 parent 74ae626 commit 1c1fb64
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,14 @@

All Notable changes to `League\Url` will be documented in this file

## 3.3.0 - 2015-03-20

### Added
- adding the `toArray` method to `League\Url\AbstractUrl` to output the URL like PHP native `parse_url` [issue #56](https://github.com/thephpleague/url/issues/56)

### Fixed
- `League\Url\Components\Query` bug fix remove parameter only if the value equals `null` [issue #58](https://github.com/thephpleague/url/issues/58)

## 3.2.1 - 2014-11-27

### Added
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -4,6 +4,7 @@
"description" : "League/url is a lightweight PHP Url manipulating library",
"keywords": ["php", "url", "parse_url"],
"license": "MIT",
"homepage" : "http://url.thephpleague.com",
"authors": [
{
"name" : "Ignace Nyamagana Butera",
Expand Down
20 changes: 20 additions & 0 deletions src/AbstractUrl.php
Expand Up @@ -164,6 +164,26 @@ public function sameValueAs(UrlInterface $url)
return $this->__toString() == $url->__toString();
}

/**
* Retuns a array representation like parse_url
* But includes all components
*
* @return array
*/
public function toArray()
{
return array(
'scheme' => $this->scheme->get(),
'user' => $this->user->get(),
'pass' => $this->pass->get(),
'host' => $this->host->get(),
'port' => $this->port->get(),
'path' => $this->path->get(),
'query' => $this->query->get(),
'fragment' => $this->fragment->get(),
);
}

/**
* Return a instance of Url from a string
*
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Query.php
Expand Up @@ -44,7 +44,7 @@ public function set($data)
$value = trim($value);
}

return null !== $value && '' !== $value;
return null !== $value;
});
}

Expand Down
15 changes: 15 additions & 0 deletions tests/AbstractUrlTest.php
Expand Up @@ -122,6 +122,21 @@ public function testCreateEmptyUrl()
$this->assertSame('', (string) Url::createFromUrl(""));
}

public function testToArray()
{
$url = Url::createFromUrl('http://example.com:80/foo/bar?foo=bar#content');
$this->assertSame(array(
'scheme' => 'http',
'user' => null,
'pass' => null,
'host' => 'example.com',
'port' => 80,
'path' => 'foo/bar',
'query' => 'foo=bar',
'fragment' => 'content',
), $url->toArray());
}

/**
* @expectedException RuntimeException
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Components/QueryTest.php
Expand Up @@ -66,6 +66,12 @@ public function testModifyWithRemoveArg()
$this->assertSame('', (string) $this->query);
}

public function testModifyWithEmptyArgument()
{
$this->query->modify(array('kingkong' => ''));
$this->assertSame('kingkong=', (string) $this->query);
}

public function testSetterWithNull()
{
$this->query->set(null);
Expand Down Expand Up @@ -138,4 +144,11 @@ public function testDotFromArray()
$query = new Query(array('foo.bar' => 'baz'));
$this->assertSame('foo.bar=baz', (string) $query);
}

public function testSameValueMerge()
{
$query = new Query("?foo=1&foo=2&bar");
$this->assertSame('2', $query['foo']);
$this->assertSame('', $query['bar']);
}
}

0 comments on commit 1c1fb64

Please sign in to comment.