Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into string
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 16 changed files with 506 additions and 126 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"homepage": "https://github.com/zendframework/zend-uri",
"autoload": {
"psr-4": {
"Zend\\Uri": "src/"
"Zend\\Uri\\": "src/"
}
},
"require": {
Expand Down
2 changes: 0 additions & 2 deletions src/Exception/ExceptionInterface.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ExceptionInterface
{}
Empty file modified src/Exception/InvalidArgumentException.php
100755 → 100644
Empty file.
2 changes: 0 additions & 2 deletions src/Exception/InvalidUriException.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidUriException extends InvalidArgumentException implements ExceptionInterface
{}
6 changes: 2 additions & 4 deletions src/Exception/InvalidUriPartException.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
* @category Zend
* @package Zend_Uri
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidUriPartException extends InvalidArgumentException
{
/**
* Part-specific error codes
*
*
* @var integer
*/
const INVALID_SCHEME = 1;
Expand All @@ -33,5 +31,5 @@ class InvalidUriPartException extends InvalidArgumentException
const INVALID_AUTHORITY = 30;
const INVALID_PATH = 32;
const INVALID_QUERY = 64;
const INVALID_FRAGMENT = 128;
const INVALID_FRAGMENT = 128;
}
6 changes: 2 additions & 4 deletions src/File.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
/**
* File URI handler
*
* The 'file:...' scheme is loosly defined in RFC-1738
* The 'file:...' scheme is loosely defined in RFC-1738
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class File extends Uri
{
static protected $validSchemes = array('file');
protected static $validSchemes = array('file');

/**
* Check if the URI is a valid File URI
Expand Down
23 changes: 19 additions & 4 deletions src/Http.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Http extends Uri
{
Expand Down Expand Up @@ -174,10 +172,27 @@ protected function parseUserInfo()
public function getPort()
{
if (empty($this->port)) {
if (array_key_exists($this->scheme, self::$defaultPorts)) {
return self::$defaultPorts[$this->scheme];
if (array_key_exists($this->scheme, static::$defaultPorts)) {
return static::$defaultPorts[$this->scheme];
}
}
return $this->port;
}

/**
* Parse a URI string
*
* @param string $uri
* @return Http
*/
public function parse($uri)
{
parent::parse($uri);

if (empty($this->path)) {
$this->path = '/';
}

return $this;
}
}
14 changes: 6 additions & 8 deletions src/Mailto.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Zend\Uri;

use Zend\Validator\ValidatorInterface,
Zend\Validator\EmailAddress as EmailValidator;
use Zend\Validator\EmailAddress as EmailValidator;
use Zend\Validator\ValidatorInterface;

/**
* "Mailto" URI handler
Expand All @@ -20,8 +20,6 @@
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Mailto extends Uri
{
Expand All @@ -36,7 +34,7 @@ class Mailto extends Uri
/**
* Check if the URI is a valid Mailto URI
*
* This applys additional specific validation rules beyond the ones
* This applies additional specific validation rules beyond the ones
* required by the generic URI syntax
*
* @return boolean
Expand All @@ -63,7 +61,7 @@ public function isValid()
/**
* Set the email address
*
* This is infact equivalent to setPath() - but provides a more clear interface
* This is in fact equivalent to setPath() - but provides a more clear interface
*
* @param string $email
* @return Mailto
Expand All @@ -88,7 +86,7 @@ public function getEmail()
/**
* Set validator to use when validating email address
*
* @param Validator $validator
* @param ValidatorInterface $validator
* @return Mailto
*/
public function setValidator(ValidatorInterface $validator)
Expand All @@ -103,7 +101,7 @@ public function setValidator(ValidatorInterface $validator)
* If none is currently set, an EmailValidator instance with default options
* will be used.
*
* @return Validator
* @return ValidatorInterface
*/
public function getValidator()
{
Expand Down
57 changes: 46 additions & 11 deletions src/Uri.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@

namespace Zend\Uri;

use Zend\Escaper\Escaper;
use Zend\Validator;

/**
* Generic URI handler
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Uri
class Uri implements UriInterface
{
/**
* Character classes defined in RFC-3986
*/
const CHAR_UNRESERVED = '\w\-\.~';
const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
const CHAR_GEN_DELIMS = ':\/\?#\[\]@';
const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
const CHAR_RESERVED = ':\/\?#\[\]@!\$&\'\(\)\*\+,;=';
Expand Down Expand Up @@ -127,6 +126,11 @@ class Uri
*/
protected static $defaultPorts = array();

/**
* @var Escaper
*/
protected static $escaper;

/**
* Create a new URI object
*
Expand All @@ -137,7 +141,7 @@ public function __construct($uri = null)
{
if (is_string($uri)) {
$this->parse($uri);
} elseif ($uri instanceof Uri) {
} elseif ($uri instanceof UriInterface) {
// Copy constructor
$this->setScheme($uri->getScheme());
$this->setUserInfo($uri->getUserInfo());
Expand All @@ -154,6 +158,31 @@ public function __construct($uri = null)
}
}

/**
* Set Escaper instance
*
* @param Escaper $escaper
*/
public static function setEscaper(Escaper $escaper)
{
static::$escaper = $escaper;
}

/**
* Retrieve Escaper instance
*
* Lazy-loads one if none provided
*
* @return Escaper
*/
public static function getEscaper()
{
if (null === static::$escaper) {
static::setEscaper(new Escaper());
}
return static::$escaper;
}

/**
* Check if the URI is valid
*
Expand Down Expand Up @@ -278,6 +307,7 @@ public function parse($uri)
$this->setPath($match[0]);
$uri = substr($uri, strlen($match[0]));
}

if (!$uri) {
return $this;
}
Expand Down Expand Up @@ -389,6 +419,7 @@ public function normalize()
}

// If path is empty (and we have a host), path should be '/'
// Isn't this valid ONLY for HTTP-URI?
if ($this->host && empty($this->path)) {
$this->path = '/';
}
Expand Down Expand Up @@ -935,8 +966,9 @@ public static function encodeUserInfo($userInfo)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:]|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $userInfo);
Expand All @@ -949,6 +981,7 @@ public static function encodeUserInfo($userInfo)
* part with percent-encoded representation
*
* @param string $path
* @throws Exception\InvalidArgumentException
* @return string
*/
public static function encodePath($path)
Expand All @@ -961,8 +994,9 @@ public static function encodePath($path)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $path);
Expand All @@ -989,8 +1023,9 @@ public static function encodeQueryFragment($input)
}

$regex = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/';
$replace = function($match) {
return rawurlencode($match[0]);
$escaper = static::getEscaper();
$replace = function ($match) use ($escaper) {
return $escaper->escapeUrl($match[0]);
};

return preg_replace_callback($regex, $replace, $input);
Expand Down
46 changes: 41 additions & 5 deletions src/UriFactory.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
* classes can be registered using the registerScheme() method.
*
* Note that this class contains only static methods and should not be
* instanciated
* instantiated
*
* @category Zend
* @package Zend_Uri
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class UriFactory
{
Expand All @@ -39,6 +37,8 @@ abstract class UriFactory
'https' => 'Zend\Uri\Http',
'mailto' => 'Zend\Uri\Mailto',
'file' => 'Zend\Uri\File',
'urn' => 'Zend\Uri\Uri',
'tag' => 'Zend\Uri\Uri',
);

/**
Expand All @@ -53,6 +53,36 @@ public static function registerScheme($scheme, $class)
static::$schemeClasses[$scheme] = $class;
}

/**
* Unregister a scheme
*
* @param string $scheme
*/
public static function unregisterScheme($scheme)
{
$scheme = strtolower($scheme);
if (isset(static::$schemeClasses[$scheme])) {
unset(static::$schemeClasses[$scheme]);
}
}

/**
* Get the class name for a registered scheme
*
* If provided scheme is not registered, will return NULL
*
* @param string $scheme
* @return string|null
*/
public static function getRegisteredSchemeClass($scheme)
{
if (isset(static::$schemeClasses[$scheme])) {
return static::$schemeClasses[$scheme];
} else {
return null;
}
}

/**
* Create a URI from a string
*
Expand All @@ -76,12 +106,18 @@ public static function factory($uriString, $defaultScheme = null)
$scheme = $defaultScheme;
}

if ($scheme && ! isset(static::$schemeClasses[$scheme])) {
throw new Exception\InvalidArgumentException(sprintf(
'no class registered for scheme "%s"',
$scheme
));
}
if ($scheme && isset(static::$schemeClasses[$scheme])) {
$class = static::$schemeClasses[$scheme];
$uri = new $class($uri);
if (! $uri instanceof Uri) {
if (! $uri instanceof UriInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'class "%s" registered for scheme "%s" is not a subclass of Zend\Uri\Uri',
'class "%s" registered for scheme "%s" does not implement Zend\Uri\UriInterface',
$class,
$scheme
));
Expand Down
Loading

0 comments on commit 6fd65f7

Please sign in to comment.