Skip to content

Commit 0c7eb6a

Browse files
authored
Allow json response from service class
1 parent 00906b9 commit 0c7eb6a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Services/StructuredDataService.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(StructuredDataParser $parser)
1717
$this->parser = $parser;
1818
}
1919

20-
public function getJsonLdScripts($item): array
20+
public function getJsonLdScripts($item, $json = false): array
2121
{
2222
$templates = $this->getTemplates($item);
2323

@@ -43,7 +43,7 @@ public function getJsonLdScripts($item): array
4343
try {
4444
$parsedSchemas = $this->parser->parse($schemas, $item);
4545
foreach ($parsedSchemas as $parsedSchema) {
46-
$scripts[] = $this->formatJsonLd($parsedSchema);
46+
$scripts[] = $this->formatJsonLd($parsedSchema, $json);
4747
}
4848
} catch (\Exception $e) {
4949
continue;
@@ -53,13 +53,17 @@ public function getJsonLdScripts($item): array
5353
return $scripts;
5454
}
5555

56-
public function formatJsonLd(array $schema): string
56+
public function formatJsonLd(array $schema, $json = false): string
5757
{
5858
$transformedSchema = $this->transformSchema($schema);
59+
$schema = json_encode($transformedSchema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
60+
if ($json) {
61+
return $schema;
62+
}
5963

6064
return sprintf(
6165
'<script type="application/ld+json">%s</script>',
62-
json_encode($transformedSchema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
66+
$schema
6367
);
6468
}
6569

0 commit comments

Comments
 (0)