PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/league/uri/src/Components/Host.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 424 lines | 176 code | 47 blank | 201 comment | 18 complexity | 05f7e5d85ff8541261397fcdf813d687 MD5 | raw file
  1. <?php
  2. /**
  3. * League.Uri (http://uri.thephpleague.com)
  4. *
  5. * @package League.uri
  6. * @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
  7. * @copyright 2013-2015 Ignace Nyamagana Butera
  8. * @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
  9. * @version 4.1.0
  10. * @link https://github.com/thephpleague/uri/
  11. */
  12. namespace League\Uri\Components;
  13. use InvalidArgumentException;
  14. use League\Uri\Interfaces\Host as HostInterface;
  15. /**
  16. * Value object representing a URI host component.
  17. *
  18. * @package League.uri
  19. * @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
  20. * @since 1.0.0
  21. */
  22. class Host extends AbstractHierarchicalComponent implements HostInterface
  23. {
  24. use HostIpTrait;
  25. use HostnameInfoTrait;
  26. use HostnameTrait;
  27. /**
  28. * HierarchicalComponent delimiter
  29. *
  30. * @var string
  31. */
  32. protected static $separator = '.';
  33. /**
  34. * Host literal representation
  35. *
  36. * @var string
  37. */
  38. protected $host;
  39. /**
  40. * DEPRECATION WARNING! This method will be removed in the next major point release
  41. *
  42. * @deprecated deprecated since version 4.2
  43. *
  44. * return a new instance from an array or a traversable object
  45. *
  46. * @param \Traversable|string[] $data The segments list
  47. * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE
  48. *
  49. * @throws InvalidArgumentException If $data is invalid
  50. * @throws InvalidArgumentException If $type is not a recognized constant
  51. *
  52. * @return static
  53. */
  54. public static function createFromArray($data, $type = self::IS_RELATIVE)
  55. {
  56. return self::createFromLabels($data, $type);
  57. }
  58. /**
  59. * return a new instance from an array or a traversable object
  60. *
  61. * @param \Traversable|string[] $data The segments list
  62. * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE
  63. *
  64. * @throws InvalidArgumentException If $data is invalid
  65. * @throws InvalidArgumentException If $type is not a recognized constant
  66. *
  67. * @return static
  68. */
  69. public static function createFromLabels($data, $type = self::IS_RELATIVE)
  70. {
  71. static $type_list = [self::IS_ABSOLUTE => 1, self::IS_RELATIVE => 1];
  72. $data = static::validateIterator($data);
  73. if (!isset($type_list[$type])) {
  74. throw new InvalidArgumentException('Please verify the submitted constant');
  75. }
  76. if ([] === $data) {
  77. return new static();
  78. }
  79. if ([''] === $data) {
  80. return new static('');
  81. }
  82. return new static(static::format($data, $type));
  83. }
  84. /**
  85. * Return a formatted host string
  86. *
  87. * @param string[] $data The segments list
  88. * @param int $type
  89. *
  90. * @return string
  91. */
  92. protected static function format(array $data, $type)
  93. {
  94. $hostname = implode(static::$separator, array_reverse($data));
  95. if (self::IS_ABSOLUTE === $type) {
  96. return $hostname.static::$separator;
  97. }
  98. return $hostname;
  99. }
  100. /**
  101. * New instance
  102. *
  103. * @param null|string $host
  104. */
  105. public function __construct($host = null)
  106. {
  107. $this->data = $this->validate($host);
  108. $this->host = !$this->isIp() ? $this->__toString() : $this->data[0];
  109. }
  110. /**
  111. * validate the submitted data
  112. *
  113. * @param string $str
  114. *
  115. * @return array
  116. */
  117. protected function validate($str)
  118. {
  119. if (null === $str) {
  120. return [];
  121. }
  122. $str = $this->validateString($str);
  123. if ('' === $str) {
  124. return [''];
  125. }
  126. $res = $this->validateIpHost($str);
  127. if (!empty($res)) {
  128. return $res;
  129. }
  130. return $this->validateStringHost($str);
  131. }
  132. /**
  133. * Return a new instance when needed
  134. *
  135. * @param array $data
  136. *
  137. * @return static
  138. */
  139. protected function newCollectionInstance(array $data)
  140. {
  141. return $this->createFromLabels($data, $this->isAbsolute);
  142. }
  143. /**
  144. * Returns whether or not the host is an IDN
  145. *
  146. * @return bool
  147. */
  148. public function isIdn()
  149. {
  150. return $this->isIdn;
  151. }
  152. /**
  153. * Returns whether or not the host is an IP address
  154. *
  155. * @return bool
  156. */
  157. public function isIp()
  158. {
  159. return $this->hostAsIpv4 || $this->hostAsIpv6;
  160. }
  161. /**
  162. * Returns whether or not the host is an IPv4 address
  163. *
  164. * @return bool
  165. */
  166. public function isIpv4()
  167. {
  168. return $this->hostAsIpv4;
  169. }
  170. /**
  171. * Returns whether or not the host is an IPv6 address
  172. *
  173. * @return bool
  174. */
  175. public function isIpv6()
  176. {
  177. return $this->hostAsIpv6;
  178. }
  179. /**
  180. * Returns whether or not the host has a ZoneIdentifier
  181. *
  182. * @return bool
  183. *
  184. * @see http://tools.ietf.org/html/rfc6874#section-4
  185. */
  186. public function hasZoneIdentifier()
  187. {
  188. return $this->hasZoneIdentifier;
  189. }
  190. /**
  191. * DEPRECATION WARNING! This method will be removed in the next major point release
  192. *
  193. * @deprecated deprecated since version 4.2
  194. *
  195. * Returns the instance literal representation
  196. * without encoding
  197. *
  198. * @return string
  199. */
  200. public function getLiteral()
  201. {
  202. return $this->host;
  203. }
  204. /**
  205. * DEPRECATION WARNING! This method will be removed in the next major point release
  206. *
  207. * @deprecated deprecated since version 4.2
  208. *
  209. * Returns an array representation of the host
  210. *
  211. * @return array
  212. */
  213. public function toArray()
  214. {
  215. return $this->getLabels();
  216. }
  217. /**
  218. * Returns an array representation of the Host
  219. *
  220. * @return array
  221. */
  222. public function getLabels()
  223. {
  224. return $this->convertToAscii($this->data, !$this->isIdn);
  225. }
  226. /**
  227. * Retrieves a single host label.
  228. *
  229. * Retrieves a single host label. If the label offset has not been set,
  230. * returns the default value provided.
  231. *
  232. * @param string $offset the label offset
  233. * @param mixed $default Default value to return if the offset does not exist.
  234. *
  235. * @return mixed
  236. */
  237. public function getLabel($offset, $default = null)
  238. {
  239. if (isset($this->data[$offset])) {
  240. return $this->isIdn ? rawurldecode($this->data[$offset]) : idn_to_ascii($this->data[$offset]);
  241. }
  242. return $default;
  243. }
  244. /**
  245. * @inheritdoc
  246. */
  247. public function getContent()
  248. {
  249. if ([] === $this->data) {
  250. return null;
  251. }
  252. if ($this->isIp()) {
  253. return $this->formatIp($this->data[0]);
  254. }
  255. return $this->format($this->getLabels(), $this->isAbsolute);
  256. }
  257. /**
  258. * @inheritdoc
  259. */
  260. public function __debugInfo()
  261. {
  262. return ['host' => $this->getContent()];
  263. }
  264. /**
  265. * @inheritdoc
  266. */
  267. public static function __set_state(array $properties)
  268. {
  269. $host = static::createFromLabels($properties['data'], $properties['isAbsolute']);
  270. $host->hostnameInfoLoaded = $properties['hostnameInfoLoaded'];
  271. $host->hostnameInfo = $properties['hostnameInfo'];
  272. return $host;
  273. }
  274. /**
  275. * Returns a host in his punycode encoded form
  276. *
  277. * This method MUST retain the state of the current instance, and return
  278. * an instance with the host transcoded using to ascii the RFC 3492 rules
  279. *
  280. * @see http://tools.ietf.org/html/rfc3492
  281. *
  282. * @return static
  283. */
  284. public function toAscii()
  285. {
  286. if ($this->isIp() || !$this->isIdn) {
  287. return $this;
  288. }
  289. return $this->withContent($this->format(
  290. $this->convertToAscii($this->data, $this->isIdn),
  291. $this->isAbsolute
  292. ));
  293. }
  294. /**
  295. * Returns a host in his IDN form
  296. *
  297. * This method MUST retain the state of the current instance, and return
  298. * an instance with the host in its IDN form using RFC 3492 rules
  299. *
  300. * @see http://tools.ietf.org/html/rfc3492
  301. *
  302. * @return static
  303. */
  304. public function toUnicode()
  305. {
  306. if ($this->isIp() || $this->isIdn) {
  307. return $this;
  308. }
  309. return $this->withContent($this->format($this->data, $this->isAbsolute));
  310. }
  311. /**
  312. * Return an host without its zone identifier according to RFC6874
  313. *
  314. * This method MUST retain the state of the current instance, and return
  315. * an instance without the host zone identifier according to RFC6874
  316. *
  317. * @see http://tools.ietf.org/html/rfc6874#section-4
  318. *
  319. * @return static
  320. */
  321. public function withoutZoneIdentifier()
  322. {
  323. if ($this->hasZoneIdentifier) {
  324. return $this->withContent(substr($this->data[0], 0, strpos($this->data[0], '%')));
  325. }
  326. return $this;
  327. }
  328. /**
  329. * Validated the Host Label Count
  330. *
  331. * @param array $labels Host labels
  332. *
  333. * @throws InvalidArgumentException If the validation fails
  334. */
  335. protected function assertLabelsCount(array $labels)
  336. {
  337. if (127 <= count(array_merge($this->data, $labels))) {
  338. throw new InvalidArgumentException('Invalid Hostname, verify labels count');
  339. }
  340. }
  341. /**
  342. * set the FQDN property
  343. *
  344. * @param string $str
  345. *
  346. * @return string
  347. */
  348. protected function setIsAbsolute($str)
  349. {
  350. $this->isAbsolute = self::IS_RELATIVE;
  351. if ('.' === mb_substr($str, -1, 1, 'UTF-8')) {
  352. $this->isAbsolute = self::IS_ABSOLUTE;
  353. $str = mb_substr($str, 0, -1, 'UTF-8');
  354. }
  355. return $str;
  356. }
  357. /**
  358. * @inheritdoc
  359. */
  360. public function prepend($component)
  361. {
  362. return $this->createFromLabels(
  363. $this->validateComponent($component),
  364. $this->isAbsolute
  365. )->append($this->__toString());
  366. }
  367. /**
  368. * @inheritdoc
  369. */
  370. public function append($component)
  371. {
  372. return $this->createFromLabels(array_merge(
  373. iterator_to_array($this->validateComponent($component)),
  374. $this->getLabels()
  375. ), $this->isAbsolute);
  376. }
  377. }