From 69da8e13eb50ee8c2176db50775e18a4fa6714f4 Mon Sep 17 00:00:00 2001 From: Digit Date: Fri, 20 Dec 2024 10:37:38 +0200 Subject: [PATCH] Generate a string example by type and variable name. --- composer.json | 2 +- src/Describer/WithExampleGenerator.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 27f776c..428bc50 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Swagger yaml generator", "keywords": ["api", "swagger", "open auth"], "license": "MIT", - "version": "1.5.0", + "version": "1.5.1", "authors": [ { "name": "Digit", diff --git a/src/Describer/WithExampleGenerator.php b/src/Describer/WithExampleGenerator.php index 4b064e2..cded5df 100644 --- a/src/Describer/WithExampleGenerator.php +++ b/src/Describer/WithExampleGenerator.php @@ -54,7 +54,7 @@ public function example(?string $phpType, ?string $swaggerType = null, ?string $ $isArray = $this->isTypeArray($phpType); if ($rule === null || ($example = $this->exampleByRule($rule)) === null) { $typeClean = $isArray ? substr($phpType, 0, -2) : $phpType; - $example = $this->exampleByType($typeClean); + $example = $this->exampleByType($typeClean, $varName); } $example = $this->typeCastExample($example, $phpType); $example = $isArray ? [$example] : $example; @@ -141,7 +141,7 @@ protected function exampleByTypeSequential(?string $type, int $iteration = 1): m case 'double': return 1.65 * $iteration; case 'string': - $strArr = ['string', 'str value', 'str example', 'string data', 'some txt']; + $strArr = ['string', 'string value', 'string example', 'string data', 'some text']; return $this->takeFromArray($strArr, $iteration); case 'bool': case 'boolean': @@ -358,11 +358,15 @@ protected function exampleByRuleSequential(string $rule, int $iteration = 1): mi * Get example by given type * * @param string|null $type + * @param string|null $varName * @return array|int|string|null */ - protected function exampleByType(?string $type): mixed + protected function exampleByType(?string $type, ?string $varName = null): mixed { $type = is_string($type) ? $this->normalizeType($type, true) : null; + if ($type === 'string' && is_string($varName)) { + return Str::headline($varName); + } $key = $type; if (! isset($this->varsSequences[$key])) { $this->varsSequences[$key] = $this->generateExampleByTypeSequence($type, 10);