From d9d776af19fd0a191f82525991dafbb56e1bcfcb Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Fri, 14 Aug 2015 01:14:07 +0200 Subject: Links: refactor & improve URL cleanup Relates to #141 Relates to #133 Modifications - move URL cleanup to `application/Url.php` - rework the cleanup function - fragments: `#stuff` - GET parameters: `?var1=val1&var2=val2` - add documentation (APIs the params belong to) - add test coverage Reference - http://php.net/parse_url - http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring Signed-off-by: VirtualTam --- application/Url.php | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 application/Url.php (limited to 'application/Url.php') diff --git a/application/Url.php b/application/Url.php new file mode 100644 index 00000000..23356f39 --- /dev/null +++ b/application/Url.php @@ -0,0 +1,150 @@ +parts = parse_url($url); + } + + /** + * Returns a string representation of this URL + */ + public function __toString() + { + return unparse_url($this->parts); + } + + /** + * Removes undesired query parameters + */ + protected function cleanupQuery() + { + if (! isset($this->parts['query'])) { + return; + } + + $queryParams = explode('&', $this->parts['query']); + + foreach (self::$annoyingQueryParams as $annoying) { + foreach ($queryParams as $param) { + if (startsWith($param, $annoying)) { + $queryParams = array_diff($queryParams, array($param)); + continue; + } + } + } + + if (count($queryParams) == 0) { + unset($this->parts['query']); + return; + } + + $this->parts['query'] = implode('&', $queryParams); + } + + /** + * Removes undesired fragments + */ + protected function cleanupFragment() + { + if (! isset($this->parts['fragment'])) { + return; + } + + foreach (self::$annoyingFragments as $annoying) { + if (startsWith($this->parts['fragment'], $annoying)) { + unset($this->parts['fragment']); + break; + } + } + } + + /** + * Removes undesired query parameters and fragments + * + * @return string the string representation of this URL after cleanup + */ + public function cleanup() + { + $this->cleanupQuery(); + $this->cleanupFragment(); + return $this->__toString(); + } +} -- cgit v1.2.3