|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Script\Call; |
|
4
|
|
|
|
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
use Stringable; |
|
7
|
|
|
|
|
8
|
|
|
use function array_map; |
|
9
|
|
|
use function implode; |
|
10
|
|
|
|
|
11
|
|
|
class Func implements ParameterInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name of the javascript function |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sName; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The name of the javascript function |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $sType; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<ParameterInterface> |
|
29
|
|
|
*/ |
|
30
|
|
|
private $aArguments = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $sName The method name |
|
36
|
|
|
* @param array $aArguments The method arguments |
|
37
|
|
|
* @param bool $bHasParent |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(string $sName, array $aArguments, bool $bHasParent) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->sType = $bHasParent ? 'method' : 'func'; |
|
42
|
|
|
$this->sName = $sName; |
|
43
|
|
|
$this->aArguments = array_map(function($xArgument) { |
|
44
|
|
|
return Parameter::make($xArgument); |
|
45
|
|
|
}, $aArguments); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the type |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getType(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->sType; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
private function hasPageNumber(): bool |
|
64
|
|
|
{ |
|
65
|
|
|
foreach($this->aArguments as $xArgument) |
|
66
|
|
|
{ |
|
67
|
|
|
if($xArgument->getType() === Parameter::PAGE_NUMBER) |
|
68
|
|
|
{ |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add the page number to the function arguments. |
|
77
|
|
|
* |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
|
|
public function withPage(): self |
|
81
|
|
|
{ |
|
82
|
|
|
if(!$this->hasPageNumber()) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->aArguments[] = new Parameter(Parameter::PAGE_NUMBER, 0); |
|
85
|
|
|
} |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Convert this call to array, when converting the response into json. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function jsonSerialize(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
'_type' => $this->getType(), |
|
98
|
|
|
'_name' => $this->sName, |
|
99
|
|
|
'args' => array_map(function(JsonSerializable $xParam) { |
|
100
|
|
|
return $xParam->jsonSerialize(); |
|
101
|
|
|
}, $this->aArguments), |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns a string representation of this call |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __toString(): string |
|
111
|
|
|
{ |
|
112
|
|
|
$aArguments = array_map(function(Stringable $xParam) { |
|
113
|
|
|
return $xParam->__toString(); |
|
114
|
|
|
}, $this->aArguments); |
|
115
|
|
|
return $this->sName . '(' . implode(', ', $aArguments) . ')'; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|