Skip to content

Commit 4e92089

Browse files
committed
Add example object generation to SchemaBuilder
1 parent 20e6c12 commit 4e92089

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/Builders/SchemaBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ public function build(array $rules, array $docParams = []): array
103103
}
104104
}
105105

106+
$example = $this->buildExample($props);
107+
106108
return array_filter([
107109
'type' => 'object',
108110
'properties' => $props,
109111
'required' => $required ?: null,
112+
'example' => $example ?: null,
110113
]);
111114
}
112115

@@ -242,6 +245,29 @@ private function normalizeType(string $type): string
242245
};
243246
}
244247

248+
/**
249+
* Build example object from schema properties.
250+
*/
251+
private function buildExample(array $props): array
252+
{
253+
$example = [];
254+
255+
foreach ($props as $name => $prop) {
256+
$type = $prop['type'] ?? 'string';
257+
258+
if ($type === 'array') {
259+
$itemProps = $prop['items']['properties'] ?? null;
260+
$example[$name] = $itemProps
261+
? [$this->buildExample($itemProps)]
262+
: [$this->generateValue($name, 'string')];
263+
} else {
264+
$example[$name] = $this->generateValue($name, $type);
265+
}
266+
}
267+
268+
return $example;
269+
}
270+
245271
/**
246272
* Generate random integer between min and max.
247273
*

0 commit comments

Comments
 (0)