Skip to content

Commit 69da8e1

Browse files
committed
Generate a string example by type and variable name.
1 parent 37fe9f8 commit 69da8e1

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Swagger yaml generator",
44
"keywords": ["api", "swagger", "open auth"],
55
"license": "MIT",
6-
"version": "1.5.0",
6+
"version": "1.5.1",
77
"authors": [
88
{
99
"name": "Digit",

src/Describer/WithExampleGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function example(?string $phpType, ?string $swaggerType = null, ?string $
5454
$isArray = $this->isTypeArray($phpType);
5555
if ($rule === null || ($example = $this->exampleByRule($rule)) === null) {
5656
$typeClean = $isArray ? substr($phpType, 0, -2) : $phpType;
57-
$example = $this->exampleByType($typeClean);
57+
$example = $this->exampleByType($typeClean, $varName);
5858
}
5959
$example = $this->typeCastExample($example, $phpType);
6060
$example = $isArray ? [$example] : $example;
@@ -141,7 +141,7 @@ protected function exampleByTypeSequential(?string $type, int $iteration = 1): m
141141
case 'double':
142142
return 1.65 * $iteration;
143143
case 'string':
144-
$strArr = ['string', 'str value', 'str example', 'string data', 'some txt'];
144+
$strArr = ['string', 'string value', 'string example', 'string data', 'some text'];
145145
return $this->takeFromArray($strArr, $iteration);
146146
case 'bool':
147147
case 'boolean':
@@ -358,11 +358,15 @@ protected function exampleByRuleSequential(string $rule, int $iteration = 1): mi
358358
* Get example by given type
359359
*
360360
* @param string|null $type
361+
* @param string|null $varName
361362
* @return array|int|string|null
362363
*/
363-
protected function exampleByType(?string $type): mixed
364+
protected function exampleByType(?string $type, ?string $varName = null): mixed
364365
{
365366
$type = is_string($type) ? $this->normalizeType($type, true) : null;
367+
if ($type === 'string' && is_string($varName)) {
368+
return Str::headline($varName);
369+
}
366370
$key = $type;
367371
if (! isset($this->varsSequences[$key])) {
368372
$this->varsSequences[$key] = $this->generateExampleByTypeSequence($type, 10);

0 commit comments

Comments
 (0)