Skip to content

Commit 5914b12

Browse files
author
František Mazura
committed
add support of examples
1 parent 0301570 commit 5914b12

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,8 @@ Check `examples/cartore/CarController.php` controller and `exmples/carstore/Open
165165

166166
# TODO
167167

168-
Update `composer.json` after [pull request](https://github.com/vyuldashev/laravel-openapi/pull/37) is accepted.
168+
Update `composer.json` after [pull request](https://github.com/vyuldashev/laravel-openapi/pull/37) is accepted.
169+
170+
# Changes
171+
172+
- 2021-03-05 - support `@example`

examples/carstore/OpenApi/RequestBodies/CarValueObject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class CarValueObject
99
*
1010
* This is a car name description...
1111
*
12+
* @example My best car
13+
* @example Skoda Octavia
14+
* @example M1A1
1215
* @var string
1316
*/
1417
protected string $name;

src/DocParseHelper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ public static function getComments(string $phpDoc)
5858
return $out;
5959
}
6060

61+
public static function getExamples(string $phpDoc)
62+
{
63+
$out = null;
64+
$lines = explode("\n", $phpDoc);
65+
foreach ($lines as $line) {
66+
$line = trim($line);
67+
$line = str_replace("/**", '', $line);
68+
$line = str_replace("*/", '', $line);
69+
$line = str_replace("*", '', $line);
70+
$line = ltrim($line);
71+
preg_match("/@example (.*)/", $line, $matches);
72+
$example = $matches[1] ?? null;
73+
if (!empty($example)) {
74+
if (empty($out)) {
75+
$out = [];
76+
}
77+
$out[] = $example;
78+
}
79+
}
80+
return $out;
81+
}
6182

6283
public static function getAllParams(string $phpDoc)
6384
{
@@ -83,12 +104,16 @@ public static function getAllParams(string $phpDoc)
83104
public static function addCommentsToSchema(string $phpDoc, Schema $schema): Schema
84105
{
85106
$comments = self::getComments($phpDoc);
107+
$examples = self::getExamples($phpDoc);
86108

87109
if (!empty($comments) && sizeof($comments) > 0) {
88110
$schema = $schema->title($comments[0]);
89111
if ((sizeof($comments) > 1)) {
90112
$schema = $schema->description(implode("\n", array_slice($comments, 1)));
91113
}
114+
if (!empty($examples)) {
115+
$schema = $schema->example(implode(', ', $examples));
116+
}
92117
}
93118

94119
return $schema;

tests/CarstoreTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function testGenerate(): void
2828
[
2929
'title' => 'Car name',
3030
'description' => 'This is a car name description...',
31+
'example' => 'My best car, Skoda Octavia, M1A1',
3132
'type' => 'string',
3233
'nullable' => false,
3334
],

0 commit comments

Comments
 (0)