Open Data Catalog v2.0.0
ValidMetadataFactory.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\metastore;
4 
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use RootedData\RootedJsonData;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 
13 class ValidMetadataFactory implements ContainerInjectionInterface {
14 
20  private $schemaRetriever;
21 
27  public static function create(ContainerInterface $container) {
28  return new static(
29  $container->get('metastore.schema_retriever'),
30  );
31  }
32 
39  public function __construct(SchemaRetriever $schemaRetriever) {
40  $this->schemaRetriever = $schemaRetriever;
41  }
42 
49  public function getSchemaRetriever() {
50  return $this->schemaRetriever;
51  }
52 
68  public function get(string $json_string, $schema_id = NULL, array $options = []): RootedJsonData {
69 
70  // Add identifier for new objects if necessary.
71  if (isset($options['method']) && $options['method'] == 'POST') {
72  $data = json_decode($json_string);
73  if (!isset($data->identifier)) {
74  $json_string = $this->addIdentifier($schema_id, $json_string);
75  }
76  }
77 
78  $schema = !empty($schema_id) ? $this->getSchemaRetriever()->retrieve($schema_id) : '{}';
79  return new RootedJsonData($json_string, $schema);
80  }
81 
93  private function addIdentifier(string $schema_id, string $json_string): string {
94  $json_data = json_decode($json_string);
95  $uuid5 = new Uuid5();
96  $json_data->identifier = $uuid5->generate($schema_id, $json_string);
97  return json_encode($json_data);
98  }
99 
100 }
Drupal\metastore\ValidMetadataFactory
Definition: ValidMetadataFactory.php:13
Drupal\metastore
Drupal\metastore\ValidMetadataFactory\create
static create(ContainerInterface $container)
Definition: ValidMetadataFactory.php:27
Drupal\metastore\ValidMetadataFactory\__construct
__construct(SchemaRetriever $schemaRetriever)
Definition: ValidMetadataFactory.php:39
Drupal\metastore\Service\Uuid5
Definition: Uuid5.php:17
Drupal\metastore\SchemaRetriever
Definition: SchemaRetriever.php:13
Drupal\metastore\ValidMetadataFactory\getSchemaRetriever
getSchemaRetriever()
Definition: ValidMetadataFactory.php:49