3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator
;
13 use Zend\Uri\Exception\ExceptionInterface
as UriException
;
14 use Zend\Uri\Uri
as UriHandler
;
15 use Zend\Validator\Exception\InvalidArgumentException
;
17 class Uri
extends AbstractValidator
19 const INVALID
= 'uriInvalid';
20 const NOT_URI
= 'notUri';
25 protected $messageTemplates = array(
26 self
::INVALID
=> "Invalid type given. String expected",
27 self
::NOT_URI
=> "The input does not appear to be a valid Uri",
33 protected $uriHandler;
38 protected $allowRelative = true;
43 protected $allowAbsolute = true;
46 * Sets default option values for this instance
48 * @param array|Traversable $options
50 public function __construct($options = array())
52 if ($options instanceof Traversable
) {
53 $options = iterator_to_array($options);
54 } elseif (!is_array($options)) {
55 $options = func_get_args();
56 $temp['uriHandler'] = array_shift($options);
57 if (!empty($options)) {
58 $temp['allowRelative'] = array_shift($options);
60 if (!empty($options)) {
61 $temp['allowAbsolute'] = array_shift($options);
67 if (isset($options['uriHandler'])) {
68 $this->setUriHandler($options['uriHandler']);
70 if (isset($options['allowRelative'])) {
71 $this->setAllowRelative($options['allowRelative']);
73 if (isset($options['allowAbsolute'])) {
74 $this->setAllowAbsolute($options['allowAbsolute']);
77 parent
::__construct($options);
81 * @throws InvalidArgumentException
84 public function getUriHandler()
86 if (null === $this->uriHandler
) {
87 // Lazy load the base Uri handler
88 $this->uriHandler
= new UriHandler();
89 } elseif (is_string($this->uriHandler
) && class_exists($this->uriHandler
)) {
90 // Instantiate string Uri handler that references a class
91 $this->uriHandler
= new $this->uriHandler
;
94 if (! $this->uriHandler
instanceof UriHandler
) {
95 throw new InvalidArgumentException('URI handler is expected to be a Zend\Uri\Uri object');
98 return $this->uriHandler
;
102 * @param UriHandler $uriHandler
103 * @throws InvalidArgumentException
106 public function setUriHandler($uriHandler)
108 if (! is_subclass_of($uriHandler, 'Zend\Uri\Uri')) {
109 throw new InvalidArgumentException('Expecting a subclass name or instance of Zend\Uri\Uri as $uriHandler');
112 $this->uriHandler
= $uriHandler;
117 * Returns the allowAbsolute option
121 public function getAllowAbsolute()
123 return $this->allowAbsolute
;
127 * Sets the allowAbsolute option
129 * @param bool $allowAbsolute
132 public function setAllowAbsolute($allowAbsolute)
134 $this->allowAbsolute
= (bool) $allowAbsolute;
139 * Returns the allowRelative option
143 public function getAllowRelative()
145 return $this->allowRelative
;
149 * Sets the allowRelative option
151 * @param bool $allowRelative
154 public function setAllowRelative($allowRelative)
156 $this->allowRelative
= (bool) $allowRelative;
161 * Returns true if and only if $value validates as a Uri
163 * @param string $value
166 public function isValid($value)
168 if (!is_string($value)) {
169 $this->error(self
::INVALID
);
173 $uriHandler = $this->getUriHandler();
175 $uriHandler->parse($value);
176 if ($uriHandler->isValid()) {
177 // It will either be a valid absolute or relative URI
178 if (($this->allowRelative
&& $this->allowAbsolute
)
179 ||
($this->allowAbsolute
&& $uriHandler->isAbsolute())
180 ||
($this->allowRelative
&& $uriHandler->isValidRelative())
185 } catch (UriException
$ex) {
186 // Error parsing URI, it must be invalid
189 $this->error(self
::NOT_URI
);