Skip to content

Commit 8341c4c

Browse files
committed
Tweak SpecParser template
1 parent a3cac7d commit 8341c4c

File tree

2 files changed

+93
-91
lines changed

2 files changed

+93
-91
lines changed

util/SpecParser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ function generateTemplate($path, $template)
179179

180180
$data['url']['processed'] = processURLPaths($data);
181181
$data['url']['default'] = getDefaultPath($data['url']['processed'][count($data['url']['processed'])-1]);
182-
182+
if (count($data['methods']) > 1) {
183+
if (in_array("GET", $data['methods'])) {
184+
$data['methods'] = "GET";
185+
} else {
186+
$data['methods'] = $data['methods'][0];
187+
}
188+
}
183189
$renderVars = array(
184190
'json' => $json,
185191
'data' => $data,

util/templates/endpoint.twig

Lines changed: 86 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,153 +2,149 @@
22
33
namespace Elasticsearch\Endpoints{% for class in namespace %}{{ loop.last ? ';' : '\\' ~ class|title }}{% endfor %}
44
5-
65
use Elasticsearch\Endpoints\AbstractEndpoint;
76
use Elasticsearch\Common\Exceptions;
87
98
/**
10-
* Class {{ className|title }}
9+
* Class {{ className|title|replace({'.': ""}) }}
1110
*
1211
* @category Elasticsearch
1312
* @package Elasticsearch\Endpoints{% for class in namespace %}{{ loop.last ? '' : '\\' ~ class|title }}{% endfor %}
13+
1414
*
15-
* @author Zachary Tong <zachary.tong@elasticsearch.com>
16-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
17-
* @link http://elasticsearch.org
18-
*/
15+
* @author Zachary Tong <zach@elastic.co>
16+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
17+
* @link http://elastic.co
18+
*/
1919
2020
class {{ className|title }} extends AbstractEndpoint
2121
{
2222
{% for part, info in data.url.parts %}
23-
{% if part != 'index' and part != 'type' and part != 'id' %}
24-
// {{info.description }}
25-
private ${{part}};
26-
27-
28-
{% endif %}
23+
{% if part != 'index' and part != 'type' and part != 'id' %}
24+
// {{info.description }}
25+
private ${{part}};
26+
{% endif %}
2927
{% endfor %}
3028
{% if data.body is not null %}
3129
/**
32-
* @param array $body
33-
*
34-
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
35-
* @return $this
36-
*/
30+
* @param array $body
31+
*
32+
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
33+
* @return $this
34+
*/
3735
public function setBody($body)
3836
{
39-
if (isset($body) !== true) {
40-
return $this;
41-
}
37+
if (isset($body) !== true) {
38+
return $this;
39+
}
4240
43-
if (is_array($body) !== true) {
44-
throw new Exceptions\InvalidArgumentException(
45-
'Body must be an array'
46-
);
47-
}
48-
$this->body = $body;
49-
return $this;
41+
$this->body = $body;
42+
return $this;
5043
}
5144
5245
5346
5447
{% endif %}
5548
{% for part, info in data.url.parts %}
56-
{% if part != 'index' and part != 'type' and part != 'id' %}
57-
/**
58-
* @param ${{part}}
59-
*
60-
* @return $this
61-
*/
62-
public function set{{part|title}}(${{part}})
63-
{
49+
{% if part != 'index' and part != 'type' and part != 'id' %}
50+
/**
51+
* @param ${{part}}
52+
*
53+
* @return $this
54+
*/
55+
public function set{{part|title|replace({'_': ""})}}(${{part}})
56+
{
6457
if (isset(${{part}}) !== true) {
65-
return $this;
58+
return $this;
6659
}
67-
60+
{% if info.type == 'list' %}
61+
if (is_array(${{part}}) === true) {
62+
${{part}} = implode(",", ${{part}});
63+
}
64+
{% endif %}
6865
$this->{{part}} = ${{part}};
6966
return $this;
70-
}
71-
67+
}
7268
73-
{% endif %}
69+
{% endif %}
7470
{% endfor %}
7571
{% set exception = '' %}
76-
/**
77-
{% for part, info in data.url.parts %}
78-
{% if info.required is not null %}
79-
{% set exception = ' * @throws \\Elasticsearch\\Common\\Exceptions\\BadMethodCallException
80-
' %}
81-
{% endif %}
82-
{% endfor %}{% autoescape false %}{{ exception }}{% endautoescape %}
83-
* @return string
84-
*/
85-
protected function getURI()
86-
{
72+
/**
73+
{% for part, info in data.url.parts %}{% if info.required is not null %}{% set exception = ' * @throws \\Elasticsearch\\Common\\Exceptions\\BadMethodCallException
74+
' %}{% endif %}{% endfor %}
75+
{% autoescape false %}{{ exception }}{% endautoescape %}
76+
* @return string
77+
*/
78+
protected function getURI()
79+
{
8780
{% for part, info in data.url.parts %}
88-
{% if info.required == true %}
81+
{% if info.required == true %}
8982
if (isset($this->{{ part }}) !== true) {
90-
throw new Exceptions\RuntimeException(
91-
'{{ part }} is required for {{ className }}'
92-
);
83+
throw new Exceptions\RuntimeException(
84+
'{{ part }} is required for {{ className }}'
85+
);
9386
}
94-
{% endif %}
87+
{% endif %}
9588
{% endfor %}
9689
{% for part, info in data.url.parts %}
97-
${{ part }} = $this->{{ part }};
90+
${{ part }} = $this->{{ part }};
9891
{% endfor %}
99-
$uri = "{{ data.url.default }}";
92+
$uri = "{{ data.url.default }}";
10093
{% set loopCounter = 0 %}
10194
{% for part, info in data.url.processed %}
102-
{% if info.count > 0 %}
103-
{% set counter = 0 %}
104-
{% if loopCounter != 0 %}else{% set loopCounter = 1 %}{% endif %}if ({% for param in info.params %}{% if counter == 0 %}isset(${{ param }}) === true{% else %} && isset(${{ param }}) === true{% endif %}{% set counter = counter + 1 %}{% endfor %}) {
105-
$uri = "{{ info.parsedPath }}";
95+
{% if info.count > 0 %}
96+
{% set counter = 0 %}
97+
{% if loopCounter != 0 %}
98+
else{% endif %}if ({% for param in info.params %}{% if counter == 0 %}isset(${{ param }}) === true{% else %} && isset(${{ param }}) === true{% endif %}{% set counter = counter + 1 %}{% endfor %})
99+
{
100+
$uri = "{{ info.parsedPath }}";
106101
}
107-
{% endif %}
102+
{% set loopCounter = 1 %}
103+
{% endif %}
108104
{% endfor %}
109105
110-
return $uri;
111-
}
106+
return $uri;
107+
}
112108
113109
114-
/**
115-
* @return string[]
116-
*/
117-
protected function getParamWhitelist()
118-
{
119-
return array(
110+
/**
111+
* @return string[]
112+
*/
113+
protected function getParamWhitelist()
114+
{
115+
return array(
120116
{% for param, options in data.url.params %}
121-
'{{ param }}',
117+
'{{ param }}',
122118
{% endfor %}
123-
);
124-
}
119+
);
120+
}
125121
126122
127123
{% if data.body.required == true %}
128124
/**
129-
* @return array
130-
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
131-
*/
125+
* @return array
126+
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
127+
*/
132128
protected function getBody()
133129
{
134-
if (isset($this->body) !== true) {
135-
throw new Exceptions\RuntimeException('Body is required for Put');
136-
}
137-
return $this->body;
130+
if (isset($this->body) !== true) {
131+
throw new Exceptions\RuntimeException('Body is required for {{ className|title }}');
132+
}
133+
return $this->body;
138134
}
139135
140136
141137
{% endif %}
142-
/**
143-
* @return string
144-
*/
145-
protected function getMethod()
146-
{
138+
/**
139+
* @return string
140+
*/
141+
protected function getMethod()
142+
{
147143
{% if data.methods|length > 1 %}
148-
//TODO Fix Me!
149-
return '{{ data.methods|join(',') }}';
144+
//TODO Fix Me!
145+
return '{{ data.methods|join(',') }}';
150146
{% else %}
151-
return '{{ data.methods[0] }}';
147+
return '{{ data.methods[0] }}';
152148
{% endif %}
153-
}
149+
}
154150
}

0 commit comments

Comments
 (0)