CallTrait.php 1.6 KB

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