1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace carono\turbotext\codegen; |
4
|
|
|
|
5
|
|
|
use carono\codegen\ClassGenerator; |
6
|
|
|
|
7
|
|
|
class RequestAbstract extends ClassGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
protected function formExtends() |
13
|
|
|
{ |
14
|
|
|
return 'carono\turbotext\RequestAbstract'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
protected function formClassName() |
21
|
|
|
{ |
22
|
|
|
return ucfirst($this->params['name']) . 'Request'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
protected function formClassNamespace() |
29
|
|
|
{ |
30
|
|
|
return 'carono\turbotext\request'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function formOutputPath() |
37
|
|
|
{ |
38
|
|
|
return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'request' . DIRECTORY_SEPARATOR . $this->formClassName() . '.php'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function methods() |
45
|
|
|
{ |
46
|
|
|
foreach ($this->params['methods'] as $methodData) { |
47
|
|
|
$methodName = formMethodName($methodData['name']); |
48
|
|
|
$method = $this->phpClass->addMethod($methodName); |
49
|
|
|
$method->addComment(stripAndWordWrap($methodData['description'])); |
50
|
|
|
$params = []; |
51
|
|
|
$params['action'] = $methodData['name']; |
52
|
|
|
if (count($methodData['params']) > 4) { |
53
|
|
|
$config = new ConfigAbstract(); |
54
|
|
|
$config->renderToFile($methodData); |
55
|
|
|
$className = '\\' . $config->formClassNamespace() . '\\' . $config->formClassName(); |
56
|
|
|
$method->addParameter('config'); |
57
|
|
|
$method->addComment("@param $className|array \$config"); |
58
|
|
|
|
59
|
|
|
$body = <<<PHP |
60
|
|
|
foreach ((\$config instanceof \carono\\turbotext\ConfigAbstract ? \$config->toArray() : \$config) as \$key => \$value) { |
61
|
|
|
\$params[\$key] = \$value; |
62
|
|
|
} |
63
|
|
|
PHP; |
64
|
|
|
$paramsStr = self::arrayAsPhpVar($params); |
65
|
|
|
$method->addBody("\$params = $paramsStr;"); |
66
|
|
|
$method->addBody($body); |
67
|
|
|
} else { |
68
|
|
|
foreach ($methodData['params'] as $param) { |
69
|
|
|
$type = formParamType($param['type']); |
70
|
|
|
$description = trim(stripAndWordWrap($param['description'])); |
71
|
|
|
$method->addComment("@param {$type} \${$param['name']} {$description}"); |
72
|
|
|
$params[$param['name']] = '$' . $param['name']; |
73
|
|
|
if ($param['required']) { |
74
|
|
|
$method->addParameter($param['name']); |
75
|
|
|
} else { |
76
|
|
|
$method->addParameter($param['name'], null); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$paramsStr = self::arrayAsPhpVar($params); |
80
|
|
|
$method->addBody("\$params = $paramsStr;"); |
81
|
|
|
} |
82
|
|
|
$response = new ResponseAbstract(); |
83
|
|
|
if (isset($methodData['returns'][0]['result'])) { |
84
|
|
|
$returns = $methodData['returns'][0]; |
85
|
|
|
$response->renderToFile($returns); |
86
|
|
|
$className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
87
|
|
|
} elseif ($methodData['returns']) { |
88
|
|
|
$methodData['returns']['name'] = $methodData['name']; |
89
|
|
|
$response->properties['asd'] = 213; |
90
|
|
|
$response->renderToFile($methodData['returns']); |
91
|
|
|
$className = $response->formClassNamespace() . '\\' . $response->formClassName(); |
92
|
|
|
} else { |
93
|
|
|
$className = 'carono\turbotext\Response'; |
94
|
|
|
} |
95
|
|
|
$method->addComment("@return \\$className|string|\stdClass|\SimpleXMLElement"); |
96
|
|
|
$method->addBody("return \$this->getClient()->getContent('api', \$params, '$className');"); |
97
|
|
|
} |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected static function arrayAsPhpVar($array) |
102
|
|
|
{ |
103
|
|
|
$export = []; |
104
|
|
|
foreach ($array as $key => $value) { |
105
|
|
|
$export[] = "'$key' => " . (strpos($value, '$') === false ? "'$value'" : $value); |
106
|
|
|
} |
107
|
|
|
$export = join(",\n\t", $export); |
108
|
|
|
if ($array) { |
109
|
|
|
$result = "[\n\t$export\n]"; |
110
|
|
|
} else { |
111
|
|
|
$result = "[]"; |
112
|
|
|
} |
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
} |