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

Commit

Permalink
Zend\Uri: Completed mailto implementation
Browse files Browse the repository at this point in the history
- Added validation to Mailto implementation
- Added tests for Mailto behaviors
- Minor CS cleanup of all URI classes (s/static (protected|public)/\1 static/g)
  • Loading branch information
weierophinney committed Jul 21, 2011
1 parent 6775f16 commit 61f8e53
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class Http extends Uri
/**
* @see Uri::$validSchemes
*/
static protected $validSchemes = array('http', 'https');
protected static $validSchemes = array('http', 'https');

/**
* @see Uri::$defaultPorts
*/
static protected $defaultPorts = array(
protected static $defaultPorts = array(
'http' => 80,
'https' => 443,
);
Expand Down Expand Up @@ -141,7 +141,7 @@ public function setPassword($password)
* @param integer $allowed
* @return boolean
*/
static public function validateHost($host, $allowed = self::HOST_DNSORIPV4)
public static function validateHost($host, $allowed = self::HOST_DNSORIPV4)
{
return parent::validateHost($host, $allowed);
}
Expand Down
54 changes: 52 additions & 2 deletions src/Mailto.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
namespace Zend\Uri;

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

/**
* "Mailto" URI handler
*
Expand All @@ -35,7 +38,13 @@
*/
class Mailto extends Uri
{
static protected $validSchemes = array('mailto');
protected static $validSchemes = array('mailto');

/**
* Validator for use when validating email address
* @var Validator
*/
protected $emailValidator;

/**
* Check if the URI is a valid Mailto URI
Expand All @@ -48,7 +57,20 @@ class Mailto extends Uri
*/
public function isValid()
{

if ($this->host || $this->userInfo || $this->port) {
return false;
}

if (empty($this->path)) {
return false;
}

if (0 === strpos($this->path, '/')) {
return false;
}

$validator = $this->getValidator();
return $validator->isValid($this->path);
}

/**
Expand All @@ -75,4 +97,32 @@ public function getEmail()
{
return $this->getPath();
}

/**
* Set validator to use when validating email address
*
* @param Validator $validator
* @return Mailto
*/
public function setValidator(Validator $validator)
{
$this->emailValidator = $validator;
return $this;
}

/**
* Retrieve validator for use with validating email address
*
* If none is currently set, an EmailValidator instance with default options
* will be used.
*
* @return Validator
*/
public function getValidator()
{
if (null === $this->emailValidator) {
$this->setValidator(new EmailValidator());
}
return $this->emailValidator;
}
}
48 changes: 24 additions & 24 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Uri
*
* @var array
*/
static protected $validSchemes = array();
protected static $validSchemes = array();

/**
* List of default ports per scheme
Expand All @@ -132,7 +132,7 @@ class Uri
*
* @var array
*/
static protected $defaultPorts = array();
protected static $defaultPorts = array();

/**
* Create a new URI object
Expand Down Expand Up @@ -764,7 +764,7 @@ public function __toString()
* @param string $scheme
* @return boolean
*/
static public function validateScheme($scheme)
public static function validateScheme($scheme)
{
if (!empty(static::$validSchemes)
&& !in_array(strtolower($scheme), static::$validSchemes)
Expand All @@ -781,7 +781,7 @@ static public function validateScheme($scheme)
* @param string $userInfo
* @return boolean
*/
static public function validateUserInfo($userInfo)
public static function validateUserInfo($userInfo)
{
$regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':]+|%[A-Fa-f0-9]{2})*$/';
return (boolean) preg_match($regex, $userInfo);
Expand All @@ -804,7 +804,7 @@ static public function validateUserInfo($userInfo)
* @param integer $allowed bitmask of allowed host types
* @return boolean
*/
static public function validateHost($host, $allowed = self::HOST_ALL)
public static function validateHost($host, $allowed = self::HOST_ALL)
{
if ($allowed & self::HOST_REGNAME) {
if (static::isValidRegName($host)) {
Expand Down Expand Up @@ -835,7 +835,7 @@ static public function validateHost($host, $allowed = self::HOST_ALL)
* @param integer $port
* @return boolean
*/
static public function validatePort($port)
public static function validatePort($port)
{
if ($port === 0) {
return false;
Expand All @@ -857,7 +857,7 @@ static public function validatePort($port)
* @param string $path
* @return boolean
*/
static public function validatePath($path)
public static function validatePath($path)
{
$pchar = '(?:[' . self::CHAR_UNRESERVED . ':@&=\+\$,]+|%[A-Fa-f0-9]{2})*';
$segment = $pchar . "(?:;{$pchar})*";
Expand All @@ -877,7 +877,7 @@ static public function validatePath($path)
* @param string $input
* @return boolean
*/
static public function validateQueryFragment($input)
public static function validateQueryFragment($input)
{
$regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':@\/\?]+|%[A-Fa-f0-9]{2})*$/';
return (boolean) preg_match($regex, $input);
Expand All @@ -890,7 +890,7 @@ static public function validateQueryFragment($input)
* @return string
* @throws Exception\InvalidArgumentException
*/
static public function encodeUserInfo($userInfo)
public static function encodeUserInfo($userInfo)
{
if (!is_string($userInfo)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -916,7 +916,7 @@ static public function encodeUserInfo($userInfo)
* @param string $path
* @return string
*/
static public function encodePath($path)
public static function encodePath($path)
{
if (!is_string($path)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -943,7 +943,7 @@ static public function encodePath($path)
* @param string $input
* @return string
*/
static public function encodeQueryFragment($input)
public static function encodeQueryFragment($input)
{
if (!is_string($input)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand Down Expand Up @@ -974,7 +974,7 @@ static public function encodeQueryFragment($input)
* @return string|null
* @throws InvalidArgumentException
*/
static public function parseScheme($uriString)
public static function parseScheme($uriString)
{
if (! is_string($uriString)) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -1001,7 +1001,7 @@ static public function parseScheme($uriString)
* @param string $path
* @return string
*/
static public function removePathDotSegments($path)
public static function removePathDotSegments($path)
{
$output = '';

Expand Down Expand Up @@ -1061,7 +1061,7 @@ static public function removePathDotSegments($path)
* @param Uri|string $relativeUri
* @return Uri
*/
static public function merge($baseUri, $relativeUri)
public static function merge($baseUri, $relativeUri)
{
$uri = new self($relativeUri);
return $uri->resolve($baseUri);
Expand All @@ -1074,7 +1074,7 @@ static public function merge($baseUri, $relativeUri)
* @param integer $allowed allowed address types
* @return boolean
*/
static protected function isValidIpAddress($host, $allowed)
protected static function isValidIpAddress($host, $allowed)
{
$validatorParams = array(
'allowipv4' => (bool) ($allowed & self::HOST_IPV4),
Expand Down Expand Up @@ -1109,7 +1109,7 @@ static protected function isValidIpAddress($host, $allowed)
* @param string $host
* @return boolean
*/
static protected function isValidDnsHostname($host)
protected static function isValidDnsHostname($host)
{
$validator = new Validator\Hostname(array(
'allow' => Validator\Hostname::ALLOW_DNS | Validator\Hostname::ALLOW_LOCAL,
Expand All @@ -1124,7 +1124,7 @@ static protected function isValidDnsHostname($host)
* @param string $host
* @return boolean
*/
static protected function isValidRegName($host)
protected static function isValidRegName($host)
{
$regex = '/^(?:[' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . ':@\/\?]+|%[A-Fa-f0-9]{2})+$/';
return (bool) preg_match($regex, $host);
Expand All @@ -1146,7 +1146,7 @@ static protected function isValidRegName($host)
* @param string $scheme
* @return string
*/
static protected function normalizeScheme($scheme)
protected static function normalizeScheme($scheme)
{
return strtolower($scheme);
}
Expand All @@ -1159,7 +1159,7 @@ static protected function normalizeScheme($scheme)
* @param string $host
* @return string
*/
static protected function normalizeHost($host)
protected static function normalizeHost($host)
{
return strtolower($host);
}
Expand All @@ -1174,7 +1174,7 @@ static protected function normalizeHost($host)
* @param string $scheme
* @return integer|null
*/
static protected function normalizePort($port, $scheme = null)
protected static function normalizePort($port, $scheme = null)
{
if ($scheme
&& isset(static::$defaultPorts[$scheme])
Expand All @@ -1195,7 +1195,7 @@ static protected function normalizePort($port, $scheme = null)
* @param string $path
* @return string
*/
static protected function normalizePath($path)
protected static function normalizePath($path)
{
$path = self::encodePath(
self::decodeUrlEncodedChars(
Expand All @@ -1216,7 +1216,7 @@ static protected function normalizePath($path)
* @param string $query
* @return string
*/
static protected function normalizeQuery($query)
protected static function normalizeQuery($query)
{
$query = self::encodeQueryFragment(
self::decodeUrlEncodedChars(
Expand All @@ -1236,7 +1236,7 @@ static protected function normalizeQuery($query)
* @param string $fragment
* @return string
*/
static protected function normalizeFragment($fragment)
protected static function normalizeFragment($fragment)
{
return static::normalizeQuery($fragment);
}
Expand All @@ -1249,7 +1249,7 @@ static protected function normalizeFragment($fragment)
* @param string $input
* @param string $allowed Pattern of allowed characters
*/
static protected function decodeUrlEncodedChars($input, $allowed = '')
protected static function decodeUrlEncodedChars($input, $allowed = '')
{
$decodeCb = function($match) use ($allowed) {
$char = rawurldecode($match[0]);
Expand Down
9 changes: 5 additions & 4 deletions test/HttpTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Zend Framework
*
Expand All @@ -24,7 +23,9 @@
* @namespace
*/
namespace ZendTest\Uri;
use Zend\Uri\Http as HttpUri;

use Zend\Uri\Http as HttpUri,
PHPUnit_Framework_TestCase as TestCase;

/**
* @category Zend
Expand All @@ -36,7 +37,7 @@
* @group Zend_Uri_Http
* @group Zend_Http
*/
class HttpTest extends \PHPUnit_Framework_TestCase
class HttpTest extends TestCase
{
/**
* Data Providers
Expand Down Expand Up @@ -105,11 +106,11 @@ public function testValidScheme($scheme)
*
* @param string $scheme
* @dataProvider invalidSchemeProvider
* @expectedException \Zend\Uri\Exception\InvalidUriPartException
*/
public function testInvalidScheme($scheme)
{
$uri = new HttpUri;
$this->setExpectedException('Zend\Uri\Exception\InvalidUriPartException');
$uri->setScheme($scheme);
}

Expand Down
Loading

0 comments on commit 61f8e53

Please sign in to comment.