Skip to content

Commit

Permalink
Imported r1035
Browse files Browse the repository at this point in the history
Added Url::setQueryParams() method as a wrapper around Url::setParts().
Made unsetting query params possible.
  • Loading branch information
AnrDaemon committed Oct 21, 2019
1 parent 4ce6341 commit 267026e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 231 deletions.
229 changes: 3 additions & 226 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,230 +1,7 @@
------------------------------------------------------------------------
r991 | anrdaemon | 2019-02-26 22:36:13 +0300 (Tue, 26 Feb 2019) | 2 lines
r1035 | anrdaemon | 2019-10-21 13:48:04 +0300 (Mon, 21 Oct 2019) | 3 lines

+ Improved documentation for Net\Url tests.

------------------------------------------------------------------------
r909 | anrdaemon | 2018-10-17 16:21:08 +0300 (Wed, 17 Oct 2018) | 2 lines

* Fixed minor typo in the docblock comment.

------------------------------------------------------------------------
r874 | anrdaemon | 2018-08-31 20:02:00 +0300 (Fri, 31 Aug 2018) | 2 lines

* Repaired initial docblock, removing legacy behavior references.

------------------------------------------------------------------------
r873 | anrdaemon | 2018-08-31 18:44:46 +0300 (Fri, 31 Aug 2018) | 2 lines

* Used assertEquals where applicable for better failure representation.

------------------------------------------------------------------------
r872 | anrdaemon | 2018-08-31 18:42:04 +0300 (Fri, 31 Aug 2018) | 3 lines

* Reordered methods.
* Added PHPUnit v7 compatible exception trap.

------------------------------------------------------------------------
r871 | anrdaemon | 2018-08-31 16:20:04 +0300 (Fri, 31 Aug 2018) | 2 lines

* Use static $url in tests.

------------------------------------------------------------------------
r857 | anrdaemon | 2018-08-28 03:26:18 +0300 (Tue, 28 Aug 2018) | 3 lines

+ Net\Url - added separate constructor to create Url from environment.
+ Added more test coverage.

------------------------------------------------------------------------
r856 | anrdaemon | 2018-08-28 03:23:50 +0300 (Tue, 28 Aug 2018) | 3 lines

* Clarified Url::_parse_url error message for incorrect argument.
* Tightened parameter validation for Url::_parse_str.

------------------------------------------------------------------------
r855 | anrdaemon | 2018-08-27 23:53:37 +0300 (Mon, 27 Aug 2018) | 2 lines

* Fix UrlTest for PHP5 compatibility.

------------------------------------------------------------------------
r854 | anrdaemon | 2018-08-27 21:06:49 +0300 (Mon, 27 Aug 2018) | 2 lines

* Reverted accidental parts ordering break from r852

------------------------------------------------------------------------
r852 | anrdaemon | 2018-08-26 04:03:37 +0300 (Sun, 26 Aug 2018) | 2 lines

* Improve Url::setParts to better handle the case where query string or fragment equals to "0".

------------------------------------------------------------------------
r851 | anrdaemon | 2018-08-26 03:49:50 +0300 (Sun, 26 Aug 2018) | 2 lines

* Use external iterator. I mean, really. Why reinvent wheels?

------------------------------------------------------------------------
r806 | anrdaemon | 2018-06-19 17:23:06 +0300 (Tue, 19 Jun 2018) | 2 lines

+ Correctly parse "+" in URL path component.

------------------------------------------------------------------------
r767 | anrdaemon | 2018-03-19 23:49:28 +0300 (Mon, 19 Mar 2018) | 2 lines

* Only confirm skipped tests for known mangled characters.

------------------------------------------------------------------------
r765 | anrdaemon | 2018-03-14 01:25:26 +0300 (Wed, 14 Mar 2018) | 2 lines

* Implement mangled characters test.

------------------------------------------------------------------------
r764 | anrdaemon | 2018-03-14 01:24:28 +0300 (Wed, 14 Mar 2018) | 2 lines

* Use more common host names.

------------------------------------------------------------------------
r763 | anrdaemon | 2018-03-14 00:50:47 +0300 (Wed, 14 Mar 2018) | 2 lines

+ Use dedicated normalization of parts array.

------------------------------------------------------------------------
r762 | anrdaemon | 2018-03-14 00:19:16 +0300 (Wed, 14 Mar 2018) | 4 lines

* Split autoload.
* Tweak tests semantics.
* Put a note about parameter names mangling for Net\Url.

------------------------------------------------------------------------
r738 | anrdaemon | 2018-03-03 22:03:36 +0300 (Sat, 03 Mar 2018) | 3 lines

= Move code to subdirectory.
+ Add some functional tests.

------------------------------------------------------------------------
r737 | anrdaemon | 2018-03-03 21:50:26 +0300 (Sat, 03 Mar 2018) | 3 lines

+ Explicitly create empty object from empty string URL.
* Lighten up creation process overall.

------------------------------------------------------------------------
r736 | anrdaemon | 2018-03-03 21:14:52 +0300 (Sat, 03 Mar 2018) | 2 lines

* Lighten up Url::setParts() logic.

------------------------------------------------------------------------
r735 | anrdaemon | 2018-03-03 21:01:46 +0300 (Sat, 03 Mar 2018) | 2 lines

* Wrap parse_str and parse_url into usable methods.

------------------------------------------------------------------------
r734 | anrdaemon | 2018-03-03 20:30:12 +0300 (Sat, 03 Mar 2018) | 3 lines

- Remove port type conversion from Url::parse()
Url::setParts() already handles it.

------------------------------------------------------------------------
r733 | anrdaemon | 2018-03-03 20:25:24 +0300 (Sat, 03 Mar 2018) | 2 lines

* Normalize internal wrappers naming.

------------------------------------------------------------------------
r731 | anrdaemon | 2018-03-01 05:08:09 +0300 (Thu, 01 Mar 2018) | 2 lines

* Swap magic and interface implementations around.

------------------------------------------------------------------------
r709 | anrdaemon | 2017-11-28 22:10:42 +0300 (Tue, 28 Nov 2017) | 2 lines

+ Cache Url::__toString() representation.

------------------------------------------------------------------------
r708 | anrdaemon | 2017-11-28 22:09:53 +0300 (Tue, 28 Nov 2017) | 2 lines

* Explicitly declare methods visibility.

------------------------------------------------------------------------
r707 | anrdaemon | 2017-11-26 23:55:15 +0300 (Sun, 26 Nov 2017) | 2 lines

* Tweak docblocks.

------------------------------------------------------------------------
r705 | anrdaemon | 2017-11-03 04:26:01 +0300 (Fri, 03 Nov 2017) | 2 lines

* Fixed ports dictionary reference.

------------------------------------------------------------------------
r701 | anrdaemon | 2017-11-03 03:12:58 +0300 (Fri, 03 Nov 2017) | 5 lines

* Add more default ports.
* Move port omission to render.
* Declare virtual class properties.
* Much more documentation.

------------------------------------------------------------------------
r697 | anrdaemon | 2017-11-01 20:05:47 +0300 (Wed, 01 Nov 2017) | 4 lines

- Less dumb Url::__toString() conversion.
Omit parts that make no sense without other parts.
Check if path is absolute when host is present.

------------------------------------------------------------------------
r696 | anrdaemon | 2017-11-01 20:00:29 +0300 (Wed, 01 Nov 2017) | 3 lines

* Decode user/pass/host/fragment
* Force port to int.

------------------------------------------------------------------------
r695 | anrdaemon | 2017-10-30 04:37:23 +0300 (Mon, 30 Oct 2017) | 3 lines

+ Much documentation.
* Rewrite constructor to make more sense.

------------------------------------------------------------------------
r694 | anrdaemon | 2017-10-29 19:44:16 +0300 (Sun, 29 Oct 2017) | 2 lines

+ Url: ArrayAccess,Iterator,Countable for query parameters.

------------------------------------------------------------------------
r693 | anrdaemon | 2017-10-29 04:29:58 +0300 (Sun, 29 Oct 2017) | 2 lines

* Normalize query array.

------------------------------------------------------------------------
r690 | anrdaemon | 2017-10-22 22:56:42 +0300 (Sun, 22 Oct 2017) | 4 lines

* Handle initial values more gracefully.
* Always decode query string.
* Remove unused class properties.

------------------------------------------------------------------------
r689 | anrdaemon | 2017-10-20 01:06:24 +0300 (Fri, 20 Oct 2017) | 2 lines

* Fix key merging.

------------------------------------------------------------------------
r688 | anrdaemon | 2017-10-20 00:48:42 +0300 (Fri, 20 Oct 2017) | 2 lines

* Improve rendering of all parts.

------------------------------------------------------------------------
r687 | anrdaemon | 2017-10-20 00:32:39 +0300 (Fri, 20 Oct 2017) | 3 lines

* Improve query part rendering.
* Improve fragment part rendering.

------------------------------------------------------------------------
r686 | anrdaemon | 2017-10-16 02:15:13 +0300 (Mon, 16 Oct 2017) | 2 lines

+ Allow isset() on Url parts.

------------------------------------------------------------------------
r685 | anrdaemon | 2017-10-15 21:50:02 +0300 (Sun, 15 Oct 2017) | 3 lines

* Decode path when parsing URL.
Leaving %2F encoded.

------------------------------------------------------------------------
r680 | anrdaemon | 2017-09-05 17:05:40 +0300 (Tue, 05 Sep 2017) | 2 lines

+ Misc\Url
+ Added Url::setQueryParams() method as a wrapper around Url::setParts().
* Made unsetting query params possible.

------------------------------------------------------------------------
38 changes: 33 additions & 5 deletions src/Net/Url.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/** URL handling class.
*
* @version SVN: $Id: Url.php 1030 2019-09-02 07:00:58Z anrdaemon $
* @version SVN: $Id: Url.php 1035 2019-10-21 10:48:04Z anrdaemon $
*/

namespace AnrDaemon\Net;
Expand Down Expand Up @@ -250,15 +250,24 @@ public function parse($url)

/** Create a new instance of the class by replacing parts in the current instance
*
* Note: This is a replacement, not merge; especially in case of a `query` part.
* Parts, whose values equal to `null` (or empty array in case of `query` part) are removed from the output.
* Same applies to null values of (top-level only for now) query params.
*
* Note: This is a replacement, not merge; especially not in case of a `query` part.
*
* Note: The `query` part is always decoded into an array.
*
* @param array $parts A set of parts to replace. Uses the same names parse_url uses.
* @param array|string $parts A part name or set of parts to replace. Uses the same names parse_url uses.
* @param ?mixed $value A value of part to replace, if first argument is part name.
* @return \AnrDaemon\Net\Url A new class instance with corresponding parts replaced.
*/
public function setParts(array $parts)
public function setParts($parts, $value = null)
{
if(!is_array($parts))
{
$parts = [$parts => $value];
}

/** Filter input array */
$parts = array_intersect_key($parts, $this->params);

Expand All @@ -274,7 +283,7 @@ public function setParts(array $parts)
{
$query = $this->_parse_str($parts['query']);
$this->_rksort($query);
$parts['query'] = $query;
$parts['query'] = array_filter($query, function($value) { return isset($value); });
}

/** Reset empty replacement parts to null
Expand Down Expand Up @@ -305,6 +314,25 @@ function(&$part, $key)
return $self;
}

/** Create a new instance of the class by replacing a set of query params in the current instance
*
* Parameters set to `null` will be removed from query block.
*
* Note: This is effectivaly a merge of existing `query` part and provided array.
*
* @see \AnrDaemon\Net\Url::setParts for more information.
*
* @param array $params A set of name-value pairs to replace.
* @return \AnrDaemon\Net\Url A new class instance with corresponding query manes replaced.
*/
public function setQueryParams(array $params)
{
/** Compose new query block */
$query = $params + $this->params["query"];

return $this->setParts("query", $query);
}

// Magic!

/** Create a representation of an URL.
Expand Down
32 changes: 32 additions & 0 deletions test/Net/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,38 @@ public function overridePartsProvider()
]),
];

$data["with unset part"] = [
[
"scheme" => "http",
"host" => "localhost",
"path" => null,
],
$this->_normalized([
"scheme" => "http",
"host" => "localhost",
]),
];

$data["with unset query param"] = [
[
"scheme" => "http",
"host" => "localhost",
"path" => "/",
"query" => [
"x" => null,
"y" => "",
],
],
$this->_normalized([
"scheme" => "http",
"host" => "localhost",
"path" => "/",
"query" => [
"y" => "",
],
]),
];

return $data;
}

Expand Down

0 comments on commit 267026e

Please sign in to comment.