Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit e7555cb

Browse files
author
Ben Batschelet
committed
Use zend-validator to validate URIs, rather than duplicating that logic locally
1 parent 9c50758 commit e7555cb

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

app/code/Magento/Integration/Test/Unit/Model/Oauth/ConsumerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Integration\Test\Unit\Model\Oauth;
77

88
use Magento\Framework\Url\Validator as UrlValidator;
9+
use Zend\Validator\Uri as ZendUriValidator;
910
use Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength;
1011

1112
/**
@@ -84,7 +85,7 @@ protected function setUp()
8485

8586
$this->keyLengthValidator = new KeyLength();
8687

87-
$this->urlValidator = new UrlValidator();
88+
$this->urlValidator = new UrlValidator(new ZendUriValidator());
8889

8990
$this->oauthDataMock = $this->createPartialMock(
9091
\Magento\Integration\Helper\Oauth\Data::class,

lib/internal/Magento/Framework/Url/Validator.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
*/
1212
namespace Magento\Framework\Url;
1313

14-
use Zend\Uri\UriFactory;
15-
1614
class Validator extends \Zend_Validate_Abstract
1715
{
1816
/**#@+
@@ -21,13 +19,20 @@ class Validator extends \Zend_Validate_Abstract
2119
const INVALID_URL = 'invalidUrl';
2220
/**#@-*/
2321

22+
/**
23+
* @var \Zend\Validator\Uri
24+
*/
25+
private $validator;
26+
2427
/**
2528
* Object constructor
2629
*/
27-
public function __construct()
30+
public function __construct(\Zend\Validator\Uri $validator)
2831
{
2932
// set translated message template
3033
$this->setMessage((string)new \Magento\Framework\Phrase("Invalid URL '%value%'."), self::INVALID_URL);
34+
$this->validator = $validator;
35+
$this->validator->setAllowRelative(false);
3136
}
3237

3338
/**
@@ -47,14 +52,12 @@ public function isValid($value)
4752
{
4853
$this->_setValue($value);
4954

50-
try {
51-
$uri = UriFactory::factory($value);
52-
if ($uri->isValid()) {
53-
return true;
54-
}
55-
} catch (Exception $e) {/** left empty */}
55+
$valid = $this->validator->isValid($value);
56+
57+
if (!$valid) {
58+
$this->_error(self::INVALID_URL);
59+
}
5660

57-
$this->_error(self::INVALID_URL);
58-
return false;
61+
return $valid;
5962
}
6063
}

0 commit comments

Comments
 (0)