Skip to content

Commit 18deb9f

Browse files
committed
feat: add a way to generate fake data in static template
1 parent 803efe6 commit 18deb9f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

components/StaticTemplatesBundle/lib/Faker/Generators/DeterminedAssociativeArrayGenerator.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,33 @@ public function generate(string $type): array
2929
$matches = [];
3030
preg_match('/^\{(.*)\}$/', $type, $matches);
3131

32-
$els = [];
33-
preg_match_all('/(\w+):([^,\s{\[]+|{\S+}(\[(\d+)?\])?|\[\S+\])/', $matches[1] ?? '', $els, PREG_SET_ORDER);
32+
$entries = [];
33+
34+
$strSplit = str_split($matches[1]);
35+
$entry = '';
36+
$isSubEl = 0;
37+
foreach ($strSplit as $char) {
38+
if (',' === $char && 0 === $isSubEl) {
39+
$entries[] = $entry;
40+
$entry = '';
41+
continue;
42+
}
43+
if (in_array($char, ['{', '['])) {
44+
++$isSubEl;
45+
}
46+
if (in_array($char, ['}', ']'])) {
47+
--$isSubEl;
48+
}
49+
$entry .= $char;
50+
}
51+
$entries[] = $entry;
3452

3553
$items = [];
36-
foreach ($els as $el) {
37-
[,$elKey, $elType] = $el;
38-
$items[trim($elKey)] = $this->generator->generate(trim($elType));
54+
foreach ($entries as $entry) {
55+
preg_match('/(\w+):(.*)$/', $entry, $matches);
56+
57+
[,$entryKey, $entryType] = $matches;
58+
$items[trim($entryKey)] = $this->generator->generate(trim($entryType));
3959
}
4060

4161
return $items;

components/StaticTemplatesBundle/lib/Templating/Twig/Extension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(Generator $generator)
3434
public function getFunctions(): array
3535
{
3636
return [
37-
new TwigFunction('generateFake', [$this, 'generateFake']),
37+
new TwigFunction('generateFake', [$this, 'generateFake'], ['is_safe' => ['html']]),
3838
];
3939
}
4040

0 commit comments

Comments
 (0)