Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / Uri.php
blob4e9c0f70cbcb2d60694f9d512b7212195bcee138
1 <?php
2 /**
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
8 */
10 namespace Zend\Validator;
12 use Traversable;
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';
22 /**
23 * @var array
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",
30 /**
31 * @var UriHandler
33 protected $uriHandler;
35 /**
36 * @var bool
38 protected $allowRelative = true;
40 /**
41 * @var bool
43 protected $allowAbsolute = true;
45 /**
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);
64 $options = $temp;
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);
80 /**
81 * @throws InvalidArgumentException
82 * @return UriHandler
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
104 * @return Uri
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;
113 return $this;
117 * Returns the allowAbsolute option
119 * @return bool
121 public function getAllowAbsolute()
123 return $this->allowAbsolute;
127 * Sets the allowAbsolute option
129 * @param bool $allowAbsolute
130 * @return Uri
132 public function setAllowAbsolute($allowAbsolute)
134 $this->allowAbsolute = (bool) $allowAbsolute;
135 return $this;
139 * Returns the allowRelative option
141 * @return bool
143 public function getAllowRelative()
145 return $this->allowRelative;
149 * Sets the allowRelative option
151 * @param bool $allowRelative
152 * @return Uri
154 public function setAllowRelative($allowRelative)
156 $this->allowRelative = (bool) $allowRelative;
157 return $this;
161 * Returns true if and only if $value validates as a Uri
163 * @param string $value
164 * @return bool
166 public function isValid($value)
168 if (!is_string($value)) {
169 $this->error(self::INVALID);
170 return false;
173 $uriHandler = $this->getUriHandler();
174 try {
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())
182 return true;
185 } catch (UriException $ex) {
186 // Error parsing URI, it must be invalid
189 $this->error(self::NOT_URI);
190 return false;