[2.1] Feature/uri normalize filter #2212

Merged
merged 4 commits into from Sep 14, 2012

Projects

None yet

7 participants

Contributor
shevron commented Aug 21, 2012

This adds a Zend\Filter that allows URI normalization. In addition to normalizing URIs based on their given scheme, it will also allow enforcing a scheme on scheme-less URIs (e.g. converting strings such as 'www.example.com' to 'http://www.example.com/').

This pull request fails (merged b1f9ff6 into e4d501f).

@bakura10 bakura10 commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
+ /**
+ * Enforced scheme for scheme-less URIs. See setEnforcedScheme docs for info
+ *
+ * @var string
+ */
+ protected $enforcedScheme = null;
+
+ /**
+ * Sets filter options
+ *
+ * @param string|array|\Zend\Config\Config $options
+ * @return void
+ */
+ public function __construct($options = null)
+ {
+ if ($options) $this->setOptions($options);
bakura10
bakura10 Aug 21, 2012 Contributor

Please break lines and wrap the statement around brackets (this won't pass PSR-2).

@bakura10 bakura10 commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
+ $this->enforcedScheme = $enforcedScheme;
+ return $this;
+ }
+
+ /**
+ * Filter the URL by normalizing it and applying a default scheme if set
+ *
+ * @param string $value
+ * @return string
+ */
+ public function filter($value)
+ {
+ $defaultScheme = $this->defaultScheme ?: $this->enforcedScheme;
+
+ // Reset default scheme if it is not a known scheme
+ if (! UriFactory::getRegisteredSchemeClass($defaultScheme)) {
bakura10
bakura10 Aug 21, 2012 Contributor

Remove space after !

@bakura10 bakura10 commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
+ *
+ * @param string $value
+ * @return string
+ */
+ public function filter($value)
+ {
+ $defaultScheme = $this->defaultScheme ?: $this->enforcedScheme;
+
+ // Reset default scheme if it is not a known scheme
+ if (! UriFactory::getRegisteredSchemeClass($defaultScheme)) {
+ $defaultScheme = null;
+ }
+
+ try {
+ $uri = UriFactory::factory($value, $defaultScheme);
+ if ($this->enforcedScheme && (! $uri->getScheme())) {
bakura10
bakura10 Aug 21, 2012 Contributor

Same. Remove space after !.

@bakura10 bakura10 commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
+ }
+
+ try {
+ $uri = UriFactory::factory($value, $defaultScheme);
+ if ($this->enforcedScheme && (! $uri->getScheme())) {
+ $this->enforceScheme($uri);
+ }
+
+ } catch (UriException $ex) {
+ // We are unable to parse / enfore scheme with the given config and input
+ return $value;
+ }
+
+ $uri->normalize();
+
+ if (! $uri->isValid()) {
bakura10
bakura10 Aug 21, 2012 Contributor

Same.

@bakura10 bakura10 commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
+ *
+ * @param Uri $uri
+ */
+ protected function enforceScheme(Uri $uri)
+ {
+ $path = $uri->getPath();
+ if (strpos($path, '/') !== false) {
+ list($host, $path) = explode('/', $path, 2);
+ $path = '/' . $path;
+ } else {
+ $host = $path;
+ $path = '';
+ }
+
+ // We have nothing to do if we have no host
+ if (! $host) return;
bakura10
bakura10 Aug 21, 2012 Contributor

Same.

@Maks3w Maks3w commented on an outdated diff Aug 21, 2012
library/Zend/Filter/UriNormalize.php
@@ -0,0 +1,134 @@
+<?php
+
+namespace Zend\Filter;
+
+use Zend\Filter\AbstractFilter,
+ Zend\Filter\Exception\InvalidArgumentException,
Maks3w
Maks3w Aug 21, 2012 Member

Please follow PSR-2 coding standards (one use statement for each import)

This pull request passes (merged 09b5907 into e4d501f).

Contributor

Good filter 👍

I remember a ZF1 proposal for Thomas W. to have a filter PrependString to enforce a prefix like "http://" for an URI, but it can be the international prefix for a phone number too I guess.

On the other hand, this kind of data must follow a specification that surely need specificity.

Member

This filter should be part of Zend\Uri

@weierophinney weierophinney added a commit that referenced this pull request Sep 14, 2012
@weierophinney weierophinney [#2212] Added suggest to composer.json
- added zend-uri as a suggested requirement
cb00ba8
@weierophinney weierophinney merged commit 09b5907 into zendframework:master Sep 14, 2012

1 check passed

default The Travis build passed
Details
@weierophinney weierophinney added a commit to zendframework/zend-uri that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/filter-uri-normalization' 914239b
@weierophinney weierophinney added a commit to zendframework/zend-filter that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/filter-uri-normalization' 0ab180c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment