Port.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Port utilities for Requests
  4. *
  5. * @package Requests\Utilities
  6. * @since 2.0.0
  7. */
  8. namespace WpOrg\Requests;
  9. use WpOrg\Requests\Exception;
  10. use WpOrg\Requests\Exception\InvalidArgument;
  11. /**
  12. * Find the correct port depending on the Request type.
  13. *
  14. * @package Requests\Utilities
  15. * @since 2.0.0
  16. */
  17. final class Port {
  18. /**
  19. * Port to use with Acap requests.
  20. *
  21. * @var int
  22. */
  23. const ACAP = 674;
  24. /**
  25. * Port to use with Dictionary requests.
  26. *
  27. * @var int
  28. */
  29. const DICT = 2628;
  30. /**
  31. * Port to use with HTTP requests.
  32. *
  33. * @var int
  34. */
  35. const HTTP = 80;
  36. /**
  37. * Port to use with HTTP over SSL requests.
  38. *
  39. * @var int
  40. */
  41. const HTTPS = 443;
  42. /**
  43. * Retrieve the port number to use.
  44. *
  45. * @param string $type Request type.
  46. * The following requests types are supported:
  47. * 'acap', 'dict', 'http' and 'https'.
  48. *
  49. * @return int
  50. *
  51. * @throws \WpOrg\Requests\Exception\InvalidArgument When a non-string input has been passed.
  52. * @throws \WpOrg\Requests\Exception When a non-supported port is requested ('portnotsupported').
  53. */
  54. public static function get($type) {
  55. if (!is_string($type)) {
  56. throw InvalidArgument::create(1, '$type', 'string', gettype($type));
  57. }
  58. $type = strtoupper($type);
  59. if (!defined("self::{$type}")) {
  60. $message = sprintf('Invalid port type (%s) passed', $type);
  61. throw new Exception($message, 'portnotsupported');
  62. }
  63. return constant("self::{$type}");
  64. }
  65. }