Skip to content

Commit

Permalink
Generate a string example by type and variable name.
Browse files Browse the repository at this point in the history
  • Loading branch information
digitv committed Dec 20, 2024
1 parent 37fe9f8 commit 69da8e1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions src/Describer/WithExampleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 69da8e1

Please sign in to comment.