VersionResolver.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace AlibabaCloud\Client\Resolver;
  3. use AlibabaCloud\Client\Exception\ClientException;
  4. /**
  5. * Class VersionResolver
  6. *
  7. * @codeCoverageIgnore
  8. * @package AlibabaCloud\Client\Resolver
  9. */
  10. abstract class VersionResolver
  11. {
  12. /**
  13. * @param string $name
  14. * @param array $arguments
  15. *
  16. * @return mixed
  17. */
  18. public static function __callStatic($name, $arguments)
  19. {
  20. return (new static())->__call($name, $arguments);
  21. }
  22. /**
  23. * @param string $version
  24. * @param array $arguments
  25. *
  26. * @return mixed
  27. * @throws ClientException
  28. */
  29. public function __call($version, $arguments)
  30. {
  31. $version = \ucfirst($version);
  32. $product = $this->getProductName();
  33. $position = strpos($product, 'Version');
  34. if ($position !== false && $position !== 0) {
  35. $product = \str_replace('Version', '', $product);
  36. }
  37. $class = "AlibabaCloud\\{$product}\\$version\\{$product}ApiResolver";
  38. if (\class_exists($class)) {
  39. return new $class();
  40. }
  41. throw new ClientException(
  42. "$product Versions contains no {$version}",
  43. 'SDK.VersionNotFound'
  44. );
  45. }
  46. /**
  47. * @return mixed
  48. * @throws ClientException
  49. */
  50. private function getProductName()
  51. {
  52. $array = \explode('\\', \get_class($this));
  53. if (isset($array[1])) {
  54. return $array[1];
  55. }
  56. throw new ClientException(
  57. 'Service name not found.',
  58. 'SDK.ServiceNotFound'
  59. );
  60. }
  61. }