Open Data Catalog v2.0.0
Dataset.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\TypedData\DataDefinition;
6 use Drupal\Core\TypedData\ListDataDefinition;
7 use Drupal\Core\TypedData\Plugin\DataType\ItemList;
8 use Drupal\Core\TypedData\TypedData;
10 
14 class Dataset extends ComplexDataFacade {
15  private $data;
16 
20  public static function definition() {
21  $definitions = [];
22 
23  /* @var $schemaRetriever SchemaRetriever */
24  $schemaRetriever = \Drupal::service("dkan_schema.schema_retriever");
25  $json = $schemaRetriever->retrieve("dataset");
26  $object = json_decode($json);
27  $properties = array_keys((array) $object->properties);
28 
29  foreach ($properties as $property) {
30  $type = $object->properties->{$property}->type;
31  $defs = self::getPropertyDefinition($type, $object, $property);
32  $definitions = array_merge($definitions, $defs);
33  }
34 
35  return $definitions;
36  }
37 
41  private static function getPropertyDefinition($type, $object, $property_name) {
42  $defs = [];
43  if (($type == "array" && isset($object->properties->{$property_name}->items->properties))
44  || $type == "object") {
45  $defs = self::getComplexPropertyDefinition($object->properties->{$property_name}, $type, $property_name);
46  }
47  else {
48  $defs[$property_name] = self::getDefinitionObject($type);
49  }
50  return $defs;
51  }
52 
56  private static function getComplexPropertyDefinition($property_items, $type, $property_name) {
57  $prefix = '';
58  $definitions = [];
59  $child_properties = [];
60  if ($type == "array" && isset($property_items->items->properties)) {
61  $prefix = $property_name . '__item__';
62  $props = $property_items->items->properties;
63  $child_properties = array_keys((array) $props);
64  }
65  elseif ($type == "object" && isset($property_items->properties)) {
66  $prefix = $property_name . '__';
67  $props = $property_items->properties;
68  $child_properties = array_keys((array) $props);
69  }
70  else {
71  $definitions[$property_name] = self::getDefinitionObject($type);
72  }
73 
74  foreach ($child_properties as $child) {
75  $definitions[$prefix . $child] = self::getDefinitionObject($type);
76  }
77  return $definitions;
78  }
79 
83  private static function getDefinitionObject($type) {
84  if ($type == "object" || $type == "any") {
85  $type = "string";
86  }
87 
88  if ($type == "array") {
89  return ListDataDefinition::create("string");
90  }
91  return DataDefinition::createFromDataType($type);
92  }
93 
97  public function __construct(string $json) {
98  $this->data = json_decode($json);
99  }
100 
106  public function get($property_name) {
107  $definitions = self::definition();
108 
109  if (!isset($definitions[$property_name])) {
110  return NULL;
111  }
112 
113  $definition = $definitions[$property_name];
114 
115  if ($definition instanceof ListDataDefinition) {
116  $property = new ItemList($definition, $property_name);
117  $values = $this->getArrayValues($property_name);
118  $property->setValue($values);
119  }
120  else {
121  $property = new class ($definition, $property_name) extends TypedData{};
122  $value = $this->getPropertyValue($property_name);
123  $property->setValue($value);
124  }
125 
126  return $property;
127  }
128 
132  private function getPropertyValue($property_name) {
133  $value = [];
134  $matches = [];
135 
136  if (preg_match('/(.*)__(.*)/', $property_name, $matches)) {
137  // Check if property corresponds to an object.
138  if (isset($matches[1])
139  && isset($this->data->{$matches[1]})
140  && isset($matches[2])
141  && isset($this->data->{$matches[1]}->{$matches[2]})) {
142  $value = $this->data->{$matches[1]}->{$matches[2]};
143  }
144  }
145  elseif (isset($this->data->{$property_name})) {
146  $value = $this->data->{$property_name};
147  }
148 
149  return $value;
150  }
151 
155  private function getArrayValues($property_name) {
156  $values = [];
157  $matches = [];
158  if (preg_match('/(.*)__item__(.*)/', $property_name, $matches)) {
159  foreach ($this->data->{$matches[1]} as $dist) {
160  $values[] = isset($dist->{$matches[2]}) ? $dist->{$matches[2]} : [];
161  }
162  }
163  elseif (isset($this->data->{$property_name})) {
164  $values = $this->data->{$property_name};
165  $values = is_string($values) ? json_decode($values) : $values;
166  }
167 
168  return $values;
169  }
170 
176  public function getProperties($include_computed = FALSE) {
177  $definitions = self::definition();
178  $properties = [];
179  foreach (array_keys($definitions) as $propertyName) {
180  $properties[$propertyName] = $this->get($propertyName);
181  }
182  return $properties;
183  }
184 
190  public function getValue() {
191  return $this->data;
192  }
193 
194 }
Drupal\dkan_search\ComplexData\Dataset\__construct
__construct(string $json)
Definition: Dataset.php:97
Drupal\dkan_search\ComplexData\Dataset\definition
static definition()
Definition: Dataset.php:20
Drupal\dkan_search\ComplexData\Dataset\getProperties
getProperties($include_computed=FALSE)
Definition: Dataset.php:176
Drupal\dkan_search\ComplexData\Dataset
Definition: Dataset.php:14
Drupal\dkan_search\ComplexData
Definition: Dataset.php:3
Drupal\dkan_search\Facade\ComplexDataFacade
Definition: ComplexDataFacade.php:13
Drupal\dkan_search\ComplexData\Dataset\getValue
getValue()
Definition: Dataset.php:190