PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-uri/src/Http.php

https://gitlab.com/daigiangaitu91/magento
PHP | 224 lines | 89 code | 28 blank | 107 comment | 7 complexity | 5a48fc133a8c7951a0c642e77757d44e MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Uri;
  10. /**
  11. * HTTP URI handler
  12. */
  13. class Http extends Uri
  14. {
  15. /**
  16. * @see Uri::$validSchemes
  17. */
  18. protected static $validSchemes = array(
  19. 'http',
  20. 'https'
  21. );
  22. /**
  23. * @see Uri::$defaultPorts
  24. */
  25. protected static $defaultPorts = array(
  26. 'http' => 80,
  27. 'https' => 443,
  28. );
  29. /**
  30. * @see Uri::$validHostTypes
  31. */
  32. protected $validHostTypes = self::HOST_DNS_OR_IPV4_OR_IPV6_OR_REGNAME;
  33. /**
  34. * User name as provided in authority of URI
  35. * @var null|string
  36. */
  37. protected $user;
  38. /**
  39. * Password as provided in authority of URI
  40. * @var null|string
  41. */
  42. protected $password;
  43. /**
  44. * Get the username part (before the ':') of the userInfo URI part
  45. *
  46. * @return string|null
  47. */
  48. public function getUser()
  49. {
  50. return $this->user;
  51. }
  52. /**
  53. * Get the password part (after the ':') of the userInfo URI part
  54. *
  55. * @return string|null
  56. */
  57. public function getPassword()
  58. {
  59. return $this->password;
  60. }
  61. /**
  62. * Get the User-info (usually user:password) part
  63. *
  64. * @return string|null
  65. */
  66. public function getUserInfo()
  67. {
  68. return $this->userInfo;
  69. }
  70. /**
  71. * Set the username part (before the ':') of the userInfo URI part
  72. *
  73. * @param string|null $user
  74. *
  75. * @return self
  76. */
  77. public function setUser($user)
  78. {
  79. $this->user = null === $user ? null : (string) $user;
  80. $this->buildUserInfo();
  81. return $this;
  82. }
  83. /**
  84. * Set the password part (after the ':') of the userInfo URI part
  85. *
  86. * @param string $password
  87. *
  88. * @return self
  89. */
  90. public function setPassword($password)
  91. {
  92. $this->password = null === $password ? null : (string) $password;
  93. $this->buildUserInfo();
  94. return $this;
  95. }
  96. /**
  97. * Set the URI User-info part (usually user:password)
  98. *
  99. * @param string|null $userInfo
  100. *
  101. * @return self
  102. *
  103. * @throws Exception\InvalidUriPartException If the schema definition does not have this part
  104. */
  105. public function setUserInfo($userInfo)
  106. {
  107. $this->userInfo = null === $userInfo ? null : (string) $userInfo;
  108. $this->parseUserInfo();
  109. return $this;
  110. }
  111. /**
  112. * Validate the host part of an HTTP URI
  113. *
  114. * This overrides the common URI validation method with a DNS or IP only
  115. * default. Users may still enforce allowing other host types.
  116. *
  117. * @param string $host
  118. * @param int $allowed
  119. * @return bool
  120. */
  121. public static function validateHost($host, $allowed = self::HOST_DNS_OR_IPV4_OR_IPV6)
  122. {
  123. return parent::validateHost($host, $allowed);
  124. }
  125. /**
  126. * Parse the user info into username and password segments
  127. *
  128. * Parses the user information into username and password segments, and
  129. * then sets the appropriate values.
  130. *
  131. * @return void
  132. */
  133. protected function parseUserInfo()
  134. {
  135. // No user information? we're done
  136. if (null === $this->userInfo) {
  137. $this->setUser(null);
  138. $this->setPassword(null);
  139. return;
  140. }
  141. // If no ':' separator, we only have a username
  142. if (false === strpos($this->userInfo, ':')) {
  143. $this->setUser($this->userInfo);
  144. $this->setPassword(null);
  145. return;
  146. }
  147. // Split on the ':', and set both user and password
  148. list($this->user, $this->password) = explode(':', $this->userInfo, 2);
  149. }
  150. /**
  151. * Build the user info based on user and password
  152. *
  153. * Builds the user info based on the given user and password values
  154. *
  155. * @return void
  156. */
  157. protected function buildUserInfo()
  158. {
  159. if (null !== $this->password) {
  160. $this->userInfo = $this->user . ':' . $this->password;
  161. } else {
  162. $this->userInfo = $this->user;
  163. }
  164. }
  165. /**
  166. * Return the URI port
  167. *
  168. * If no port is set, will return the default port according to the scheme
  169. *
  170. * @return int
  171. * @see Zend\Uri\Uri::getPort()
  172. */
  173. public function getPort()
  174. {
  175. if (empty($this->port)) {
  176. if (array_key_exists($this->scheme, static::$defaultPorts)) {
  177. return static::$defaultPorts[$this->scheme];
  178. }
  179. }
  180. return $this->port;
  181. }
  182. /**
  183. * Parse a URI string
  184. *
  185. * @param string $uri
  186. * @return Http
  187. */
  188. public function parse($uri)
  189. {
  190. parent::parse($uri);
  191. if (empty($this->path)) {
  192. $this->path = '/';
  193. }
  194. return $this;
  195. }
  196. }