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