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