VersionResolver.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * @throws ClientException
  18. */
  19. public static function __callStatic($name, $arguments)
  20. {
  21. return (new static())->__call($name, $arguments);
  22. }
  23. /**
  24. * @param string $version
  25. * @param array $arguments
  26. *
  27. * @return mixed
  28. * @throws ClientException
  29. */
  30. public function __call($version, $arguments)
  31. {
  32. $version = \ucfirst($version);
  33. $product = $this->getProductName();
  34. $position = strpos($product, 'Version');
  35. if ($position !== false && $position !== 0) {
  36. $product = \str_replace('Version', '', $product);
  37. }
  38. $class = "AlibabaCloud\\{$product}\\$version\\{$product}ApiResolver";
  39. if (\class_exists($class)) {
  40. return new $class();
  41. }
  42. throw new ClientException(
  43. "$product Versions contains no {$version}",
  44. 'SDK.VersionNotFound'
  45. );
  46. }
  47. /**
  48. * @return mixed
  49. * @throws ClientException
  50. */
  51. private function getProductName()
  52. {
  53. $array = \explode('\\', \get_class($this));
  54. if (is_array($array) && isset($array[1])) {
  55. return $array[1];
  56. }
  57. throw new ClientException(
  58. 'Service name not found.',
  59. 'SDK.ServiceNotFound'
  60. );
  61. }
  62. }