Skip to content

Commit

Permalink
Tweak SpecParser template
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfractal committed May 31, 2016
1 parent a3cac7d commit 8341c4c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 91 deletions.
8 changes: 7 additions & 1 deletion util/SpecParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ function generateTemplate($path, $template)

$data['url']['processed'] = processURLPaths($data);
$data['url']['default'] = getDefaultPath($data['url']['processed'][count($data['url']['processed'])-1]);

if (count($data['methods']) > 1) {
if (in_array("GET", $data['methods'])) {
$data['methods'] = "GET";
} else {
$data['methods'] = $data['methods'][0];
}
}
$renderVars = array(
'json' => $json,
'data' => $data,
Expand Down
176 changes: 86 additions & 90 deletions util/templates/endpoint.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,153 +2,149 @@
namespace Elasticsearch\Endpoints{% for class in namespace %}{{ loop.last ? ';' : '\\' ~ class|title }}{% endfor %}
use Elasticsearch\Endpoints\AbstractEndpoint;
use Elasticsearch\Common\Exceptions;
/**
* Class {{ className|title }}
* Class {{ className|title|replace({'.': ""}) }}
*
* @category Elasticsearch
* @package Elasticsearch\Endpoints{% for class in namespace %}{{ loop.last ? '' : '\\' ~ class|title }}{% endfor %}
*
* @author Zachary Tong <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elasticsearch.org
*/
* @author Zachary Tong <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elastic.co
*/
class {{ className|title }} extends AbstractEndpoint
{
{% for part, info in data.url.parts %}
{% if part != 'index' and part != 'type' and part != 'id' %}
// {{info.description }}
private ${{part}};
{% endif %}
{% if part != 'index' and part != 'type' and part != 'id' %}
// {{info.description }}
private ${{part}};
{% endif %}
{% endfor %}
{% if data.body is not null %}
/**
* @param array $body
*
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
* @return $this
*/
* @param array $body
*
* @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
* @return $this
*/
public function setBody($body)
{
if (isset($body) !== true) {
return $this;
}
if (isset($body) !== true) {
return $this;
}
if (is_array($body) !== true) {
throw new Exceptions\InvalidArgumentException(
'Body must be an array'
);
}
$this->body = $body;
return $this;
$this->body = $body;
return $this;
}
{% endif %}
{% for part, info in data.url.parts %}
{% if part != 'index' and part != 'type' and part != 'id' %}
/**
* @param ${{part}}
*
* @return $this
*/
public function set{{part|title}}(${{part}})
{
{% if part != 'index' and part != 'type' and part != 'id' %}
/**
* @param ${{part}}
*
* @return $this
*/
public function set{{part|title|replace({'_': ""})}}(${{part}})
{
if (isset(${{part}}) !== true) {
return $this;
return $this;
}
{% if info.type == 'list' %}
if (is_array(${{part}}) === true) {
${{part}} = implode(",", ${{part}});
}
{% endif %}
$this->{{part}} = ${{part}};
return $this;
}
}
{% endif %}
{% endif %}
{% endfor %}
{% set exception = '' %}
/**
{% for part, info in data.url.parts %}
{% if info.required is not null %}
{% set exception = ' * @throws \\Elasticsearch\\Common\\Exceptions\\BadMethodCallException
' %}
{% endif %}
{% endfor %}{% autoescape false %}{{ exception }}{% endautoescape %}
* @return string
*/
protected function getURI()
{
/**
{% for part, info in data.url.parts %}{% if info.required is not null %}{% set exception = ' * @throws \\Elasticsearch\\Common\\Exceptions\\BadMethodCallException
' %}{% endif %}{% endfor %}
{% autoescape false %}{{ exception }}{% endautoescape %}
* @return string
*/
protected function getURI()
{
{% for part, info in data.url.parts %}
{% if info.required == true %}
{% if info.required == true %}
if (isset($this->{{ part }}) !== true) {
throw new Exceptions\RuntimeException(
'{{ part }} is required for {{ className }}'
);
throw new Exceptions\RuntimeException(
'{{ part }} is required for {{ className }}'
);
}
{% endif %}
{% endif %}
{% endfor %}
{% for part, info in data.url.parts %}
${{ part }} = $this->{{ part }};
${{ part }} = $this->{{ part }};
{% endfor %}
$uri = "{{ data.url.default }}";
$uri = "{{ data.url.default }}";
{% set loopCounter = 0 %}
{% for part, info in data.url.processed %}
{% if info.count > 0 %}
{% set counter = 0 %}
{% 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 %}) {
$uri = "{{ info.parsedPath }}";
{% if info.count > 0 %}
{% set counter = 0 %}
{% if loopCounter != 0 %}
else{% endif %}if ({% for param in info.params %}{% if counter == 0 %}isset(${{ param }}) === true{% else %} && isset(${{ param }}) === true{% endif %}{% set counter = counter + 1 %}{% endfor %})
{
$uri = "{{ info.parsedPath }}";
}
{% endif %}
{% set loopCounter = 1 %}
{% endif %}
{% endfor %}
return $uri;
}
return $uri;
}
/**
* @return string[]
*/
protected function getParamWhitelist()
{
return array(
/**
* @return string[]
*/
protected function getParamWhitelist()
{
return array(
{% for param, options in data.url.params %}
'{{ param }}',
'{{ param }}',
{% endfor %}
);
}
);
}
{% if data.body.required == true %}
/**
* @return array
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
*/
* @return array
* @throws \Elasticsearch\Common\Exceptions\RuntimeException
*/
protected function getBody()
{
if (isset($this->body) !== true) {
throw new Exceptions\RuntimeException('Body is required for Put');
}
return $this->body;
if (isset($this->body) !== true) {
throw new Exceptions\RuntimeException('Body is required for {{ className|title }}');
}
return $this->body;
}
{% endif %}
/**
* @return string
*/
protected function getMethod()
{
/**
* @return string
*/
protected function getMethod()
{
{% if data.methods|length > 1 %}
//TODO Fix Me!
return '{{ data.methods|join(',') }}';
//TODO Fix Me!
return '{{ data.methods|join(',') }}';
{% else %}
return '{{ data.methods[0] }}';
return '{{ data.methods[0] }}';
{% endif %}
}
}
}

0 comments on commit 8341c4c

Please sign in to comment.