From 86364ae81439250cb98bb1c5c4d23db5f32948a7 Mon Sep 17 00:00:00 2001 From: Roman Glebov Date: Thu, 26 Sep 2019 12:15:16 +0700 Subject: [PATCH 1/3] Support RAML 1.0 resource types declaration --- src/Parser.php | 13 +++++++++++++ tests/ParseTest.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Parser.php b/src/Parser.php index 7c850584..d012d5e9 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -412,6 +412,19 @@ private function parseResourceTypes($ramlData) { if (isset($ramlData['resourceTypes'])) { $keyedResourceTypes = []; + + foreach ($ramlData['resourceTypes'] as $i => $resourceType) { + if (\is_int($i)) { + // In RAML 0.8 is an array of maps; in each map, the keys are resourceType names + foreach ($resourceType as $k => $t) { + $keyedResourceTypes[$k] = $t; + } + } else { + // In RAML 1.0 is a map where keys names become names of resource types + $keyedResourceTypes[$i] = $resourceType; + } + } + foreach ($ramlData['resourceTypes'] as $resourceType) { foreach ($resourceType as $k => $t) { $keyedResourceTypes[$k] = $t; diff --git a/tests/ParseTest.php b/tests/ParseTest.php index d9d7597f..3b130bbf 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -1187,4 +1187,43 @@ traits: $this->assertCount(1, $simpleRaml->getResourceByPath('/users')->getTraits()); $this->assertCount(2, $simpleRaml->getResourceByPath('/users')->getMethod('get')->getTraits()); } + + /** + * @test + */ + public function shouldParseResourceTypesAndTraits() + { + $traitsAndTypes = $this->parser->parse(__DIR__ . '/fixture/raml-1.0/traitsAndTypes.raml'); + + $resource = $traitsAndTypes->getResourceByUri('/test'); + $method = $resource->getMethod('get'); + $queryParameters = $method->getQueryParameters(); + $headers = $method->getHeaders(); + + $this->assertArrayHasKey('title', $queryParameters); + $this->assertArrayHasKey('numPages', $queryParameters); + $this->assertArrayHasKey('access_token', $headers); + + $this->assertEquals( + 'Return values that have their title matching the given value', + $queryParameters['title']->getDescription() + ); + $this->assertEquals('The number of pages to return', $queryParameters['numPages']->getDescription()); + $this->assertEquals( + [ + 'access_token' => NamedParameter::createFromArray( + 'access_token', + [ + 'displayName' => null, + 'description' => 'A valid access_token is required', + 'examples' => [ + '5757gh76', + ], + 'required' => 'true', + ] + ), + ], + $headers + ); + } } From 2d35ad47ce2377ae15674454f72d7c43d1cd2e66 Mon Sep 17 00:00:00 2001 From: Roman Glebov Date: Mon, 30 Sep 2019 11:53:27 +0700 Subject: [PATCH 2/3] Remove resource types duplicate processing --- src/Parser.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index d012d5e9..80648211 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -425,12 +425,6 @@ private function parseResourceTypes($ramlData) } } - foreach ($ramlData['resourceTypes'] as $resourceType) { - foreach ($resourceType as $k => $t) { - $keyedResourceTypes[$k] = $t; - } - } - foreach ($ramlData as $key => $value) { if (\mb_strpos($key, '/') === 0) { $name = (isset($value['displayName'])) ? $value['displayName'] : \mb_substr($key, 1); From 000697ec37b1f0649decd9995cdc55ae7dc60b99 Mon Sep 17 00:00:00 2001 From: Roman Glebov Date: Tue, 1 Oct 2019 12:32:18 +0700 Subject: [PATCH 3/3] Improve code readability --- src/Parser.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 80648211..4b1d9fe3 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -413,16 +413,15 @@ private function parseResourceTypes($ramlData) if (isset($ramlData['resourceTypes'])) { $keyedResourceTypes = []; - foreach ($ramlData['resourceTypes'] as $i => $resourceType) { - if (\is_int($i)) { - // In RAML 0.8 is an array of maps; in each map, the keys are resourceType names - foreach ($resourceType as $k => $t) { + foreach ($ramlData['resourceTypes'] as $key => $value) { + if ($this->isRaml08($key)) { + foreach ($value as $k => $t) { $keyedResourceTypes[$k] = $t; } - } else { - // In RAML 1.0 is a map where keys names become names of resource types - $keyedResourceTypes[$i] = $resourceType; + + continue; } + $keyedResourceTypes[$key] = $value; } foreach ($ramlData as $key => $value) { @@ -436,6 +435,15 @@ private function parseResourceTypes($ramlData) return $ramlData; } + /** + * @param string|int $key + * @return bool + */ + private function isRaml08($key) + { + return \is_int($key); + } + /** * @param string $rootDir * @return array