CallTrait.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace AlibabaCloud\Client\Resolver;
  3. use RuntimeException;
  4. use ArgumentCountError;
  5. /**
  6. * Trait CallTrait
  7. *
  8. * @codeCoverageIgnore
  9. * @package AlibabaCloud\Client\Resolver
  10. */
  11. trait CallTrait
  12. {
  13. /**
  14. * Magic method for set or get request parameters.
  15. *
  16. * @param string $name
  17. * @param mixed $arguments
  18. *
  19. * @return $this
  20. */
  21. public function __call($name, $arguments)
  22. {
  23. if (strncmp($name, 'get', 3) === 0) {
  24. $parameter = \mb_strcut($name, 3);
  25. return $this->__get($parameter);
  26. }
  27. if (strncmp($name, 'with', 4) === 0) {
  28. $parameter = \mb_strcut($name, 4);
  29. $value = $this->getCallArguments($name, $arguments);
  30. $this->data[$parameter] = $value;
  31. $this->parameterPosition()[$parameter] = $value;
  32. return $this;
  33. }
  34. if (strncmp($name, 'set', 3) === 0) {
  35. $parameter = \mb_strcut($name, 3);
  36. $with_method = "with$parameter";
  37. return $this->$with_method($this->getCallArguments($name, $arguments));
  38. }
  39. throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
  40. }
  41. /**
  42. * @param string $name
  43. * @param array $arguments
  44. * @param int $index
  45. *
  46. * @return mixed
  47. */
  48. private function getCallArguments($name, array $arguments, $index = 0)
  49. {
  50. if (!isset($arguments[$index])) {
  51. throw new ArgumentCountError("Missing arguments to method $name");
  52. }
  53. return $arguments[$index];
  54. }
  55. }