Skip to content

Instantly share code, notes, and snippets.

@shevron
Created June 18, 2012 18:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shevron/2949800 to your computer and use it in GitHub Desktop.
Save shevron/2949800 to your computer and use it in GitHub Desktop.
Zend Framework 2.0 Uri validator
<?php
namespace Zend\Validator;
class Uri extends AbstractValidator
{
const INVALID = 'uriInvalid';
protected $_messageTemplates = array(
self::INVALID => "Provided input is not a valid URL"
);
/**
* URL object to use for validation
*
* @var \Zend\Uri\Uri
*/
protected $urlObject = null;
protected $absolute = true;
public function __construct($urlObject = null, $absolute = true)
{
parent::__construct();
if (! $urlObject) {
$urlObject = new \Zend\Uri\Uri;
} else {
if (is_string($urlObject)) {
$urlObject = new $urlObject;
}
if (! $urlObject instanceof \Zend\Uri\Uri) {
throw new \InvalidArgumentException("Unexpected URI class type, expecting an instance of Zend\\Uri\\Uri");
}
}
$this->absolute = (boolean) $absolute;
$this->urlObject = $urlObject;
}
public function isValid($value)
{
try {
if ($this->urlObject->parse($value)) {
if ($this->urlObject->isValid()) {
if ((! $this->absolute) || $this->urlObject->isAbsolute()) {
return true;
}
}
}
} catch (\Zend\Uri\Exception $ex) {
// Error parsing URL, it must be invalid
}
$this->error(self::INVALID);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment