vendor/contao/core-bundle/src/Routing/ResponseContext/JsonLd/JsonLdManager.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Routing\ResponseContext\JsonLd;
  11. use Contao\ArrayUtil;
  12. use Contao\CoreBundle\Event\JsonLdEvent;
  13. use Contao\CoreBundle\Routing\ResponseContext\ResponseContext;
  14. use Spatie\SchemaOrg\Graph;
  15. use Spatie\SchemaOrg\Type;
  16. class JsonLdManager
  17. {
  18.     public const SCHEMA_ORG 'https://schema.org';
  19.     public const SCHEMA_CONTAO 'https://schema.contao.org';
  20.     /**
  21.      * @var ResponseContext
  22.      */
  23.     private $responseContext;
  24.     /**
  25.      * @var array<Graph>
  26.      */
  27.     private $graphs = [];
  28.     public function __construct(ResponseContext $responseContext)
  29.     {
  30.         $this->responseContext $responseContext;
  31.     }
  32.     public function getGraphForSchema(string $schema): Graph
  33.     {
  34.         $schema rtrim($schema'/');
  35.         if (!\array_key_exists($schema$this->graphs)) {
  36.             $this->graphs[$schema] = new Graph($schema);
  37.         }
  38.         return $this->graphs[$schema];
  39.     }
  40.     public function getGraphs(): array
  41.     {
  42.         return $this->graphs;
  43.     }
  44.     public function collectFinalScriptFromGraphs(): string
  45.     {
  46.         $data = [];
  47.         $this->responseContext->dispatchEvent(new JsonLdEvent());
  48.         foreach ($this->getGraphs() as $graph) {
  49.             $data[] = $graph->toArray();
  50.         }
  51.         // Reset graphs
  52.         $this->graphs = [];
  53.         if (=== \count($data)) {
  54.             return '';
  55.         }
  56.         ArrayUtil::recursiveKeySort($data);
  57.         return '<script type="application/ld+json">'."\n".json_encode($dataJSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE)."\n".'</script>';
  58.     }
  59.     /**
  60.      * @throws \InvalidArgumentException
  61.      */
  62.     public function createSchemaOrgTypeFromArray(array $jsonLd): Type
  63.     {
  64.         if (!isset($jsonLd['@type'])) {
  65.             throw new \InvalidArgumentException('Must provide the @type property!');
  66.         }
  67.         $schemaClass '\Spatie\SchemaOrg\\'.$jsonLd['@type'];
  68.         if (!class_exists($schemaClass)) {
  69.             throw new \InvalidArgumentException(sprintf('Unknown schema.org type "%s" provided!'$jsonLd['@type']));
  70.         }
  71.         $schema = new $schemaClass();
  72.         unset($jsonLd['@type']);
  73.         foreach ($jsonLd as $k => $v) {
  74.             if (\is_array($v) && isset($v['@type'])) {
  75.                 $v $this->createSchemaOrgTypeFromArray($v);
  76.             }
  77.             $schema->setProperty($k$v);
  78.         }
  79.         return $schema;
  80.     }
  81. }