Created
June 18, 2012 18:18
-
-
Save shevron/2949800 to your computer and use it in GitHub Desktop.
Zend Framework 2.0 Uri validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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