Endpoint.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace AlibabaCloud\Endpoint;
  3. /**
  4. * Get endpoint.
  5. */
  6. class Endpoint
  7. {
  8. const ENDPOINT_TYPE_REGIONAL = 'regional';
  9. const ENDPOINT_TYPE_CENTRAL = 'central';
  10. const REGIONAL_RULES = '<product><suffix><network>.<region_id>.aliyuncs.com';
  11. const CENTRAL_RULES = '<product><suffix><network>.aliyuncs.com';
  12. /**
  13. * @param string $product required
  14. * @param string $regionId optional It will be required when endpoint type is 'regional'
  15. * @param string $endpointType optional regional|central
  16. * @param string $network optional
  17. * @param string $suffix optional
  18. *
  19. * @throws \InvalidArgumentException
  20. *
  21. * @return string
  22. */
  23. public static function getEndpointRules($product, $regionId, $endpointType = '', $network = '', $suffix = '')
  24. {
  25. if (empty($product)) {
  26. throw new \InvalidArgumentException('Product name cannot be empty.');
  27. }
  28. $endpoint = self::REGIONAL_RULES;
  29. if (self::ENDPOINT_TYPE_REGIONAL === $endpointType) {
  30. if (empty($regionId)) {
  31. throw new \InvalidArgumentException('RegionId is empty, please set a valid RegionId');
  32. }
  33. $endpoint = self::render($endpoint, 'region_id', strtolower($regionId));
  34. } elseif (self::ENDPOINT_TYPE_CENTRAL === $endpointType) {
  35. $endpoint = self::CENTRAL_RULES;
  36. } else {
  37. throw new \InvalidArgumentException('Invalid EndpointType');
  38. }
  39. if (!empty($network) && 'public' !== $network) {
  40. $endpoint = self::render($endpoint, 'network', '-' . $network);
  41. } else {
  42. $endpoint = self::render($endpoint, 'network', '');
  43. }
  44. if (!empty($suffix)) {
  45. $endpoint = self::render($endpoint, 'suffix', '-' . $suffix);
  46. } else {
  47. $endpoint = self::render($endpoint, 'suffix', '');
  48. }
  49. return self::render($endpoint, 'product', strtolower($product));
  50. }
  51. private static function render($str, $tag, $replace)
  52. {
  53. return str_replace('<' . $tag . '>', $replace, $str);
  54. }
  55. }