Skip to content

Commit

Permalink
add RAML Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
howyi committed Sep 3, 2017
1 parent 9617d10 commit 2714044
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Lamb/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Lamb\Converter\Postman;
use Lamb\Converter\Swagger;
use Lamb\Converter\ApiBlueprint;
use Lamb\Converter\Raml;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -29,5 +30,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

Swagger::document($collection, $environment, 'sample_env', 'build');
ApiBlueprint::document($collection, 'build');
Raml::document($collection, $environment, 'sample_env', 'build');
}
}
124 changes: 124 additions & 0 deletions src/Lamb/Converter/Raml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Lamb\Converter;

use Howyi\Evi;
use Lamb\Factory\ApiStructureFactory;
use Lamb\Structure\CollectionStructure;
use Lamb\Structure\EnvironmentStructure;
use Lamb\Structure\ApiStructure;
use Lamb\Util\Checker;
use Lamb\Util\Config;
use Lamb\Util\Bracket;
use Lamb\Util\Key;
use Symfony\Component\Yaml\Yaml;

class Raml extends AbstractConverter
{
/**
* @param string $collection
* @param string|null $path
*/
public static function document(
CollectionStructure $collection,
EnvironmentStructure $environment,
string $environmentKey,
string $path = null
): string {
$environment = $environment->getEnvironmentList()[$environmentKey];

$document = self::getLine('#%RAML ' . Key::RAML_VERSION);
$document .= self::getLine('title: ' . $collection->getName());
$document .= self::getLine('baseUri: ' . $environment['host']);
$document .= self::getLine('version: ' . Config::version(), 0, 2);

$ramlList = [];
$apiList = $collection->getApiList();
foreach ($apiList as $dir => $value) {
if (!Checker::isDir($dir)) {
continue;
}
$ramlList[$dir] = [];
self::setApiList($apiList[$dir], $ramlList[$dir]);
}
$document .= Yaml::dump($ramlList, 50, 2);

$document = Bracket::RAML($document);

if (!is_null($path)) {
$filename = $collection->getName() . '.raml';
self::save($path, $filename, $document);
}
return $document;
}

private static function setApiList($apiList, &$ramlList)
{
foreach ($apiList as $dir => $value) {
if (gettype($value) === 'object' and get_class($value) === ApiStructure::class) {
$ramlList[strtolower($dir)] = self::getApi($value);
}

if (is_array($value)) {
if (Checker::isDir($dir)) {
self::setApiList($value, $ramlList[$dir]);
} else {
self::setApiList($value, $ramlList);
}
}
}
}

private static function getApi($api)
{
$apiDetail = [];

$request = $api->getRequest();
$response = $api->getResponse();

$apiDetail['description'] = $api->getDescription();

if (!empty($request->getParameter())) {
$apiDetail['queryParameters'] = [];
foreach ($request->getParameter() as $key => $value) {
$apiDetail['queryParameters'][$key] = [
'displayName' => $key,
'type' => 'string',
'description' => '',
'required' => $value['required'],
];
}
}

if (!empty($request->getHeader())) {
foreach ($request->getHeader() as $key => $value) {
if ($key === 'Content-Type') {
continue;
}
$apiDetail['headers'][$key] = [
'description' => (isset($value['description']) ? $value['description'] : ''),
'type' => 'string',
];
}
}

if (!empty($request->getBody())) {
$body = $request->getBody();
$schema = json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$apiDetail['body']['application/json']['schema'] = $schema;
$example = $request->getTestBody(Bracket::RAML);
$apiDetail['body']['application/json']['example'] = $example;
}

$apiDetail['responses']['200'] = [];
if (!empty($response->getBody())) {
$body = $response->getBody();
$schema = json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$apiDetail['responses']['200']['body']['application/json']['schema'] = $schema;
$example = $response->getTestBody(Bracket::RAML);
$apiDetail['responses']['200']['body']['application/json']['example'] = $example;
}

return $apiDetail;
}
}
9 changes: 9 additions & 0 deletions src/Lamb/Util/Bracket.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Bracket
const SWAGGER = ['{', '}'];
const TSUNG = ['%%_', '%%'];
const APIBLUEPRINT = ['{', '}'];
const RAML = ['{', '}'];

/**
* @param string $text
Expand Down Expand Up @@ -37,4 +38,12 @@ public static function APIBLUEPRINT(string $text): string
{
return str_replace(self::LAMB, self::APIBLUEPRINT, $text);
}
/**
* @param string $text
* @return string
*/
public static function RAML(string $text): string
{
return str_replace(self::LAMB, self::RAML, $text);
}
}
1 change: 1 addition & 0 deletions src/Lamb/Util/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ class Key

const POSTMAN_VERSION = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json';
const SWAGGER_VERSION = '2.0';
const RAML_VERSION = '1.0';
}
1 change: 1 addition & 0 deletions tests/Lamb/Converter/PostmanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testCollection()
\Lamb\Converter\Postman::environment($environment, 'build');
\Lamb\Converter\Swagger::document($collection, $environment, 'sample_env', 'build');
\Lamb\Converter\ApiBlueprint::document($collection, 'build');
\Lamb\Converter\RAML::document($collection, $environment, 'sample_env', 'build');
$this->assertTrue(true);
}
}

0 comments on commit 2714044

Please sign in to comment.