Open Data Catalog v2.0.0
FacetsFromContentTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\search_api\Query\QueryInterface;
6 use RootedData\RootedJsonData;
7 
14  use FacetsCommonTrait;
15 
27  private function getFacetsFromContent(array $params, QueryInterface $query) : array {
28 
29  $results = $query->execute()->getResultItems();
30 
31  $datasets = array_map(function ($datasetId) {
32  $cleanId = str_replace("dkan_dataset/", "", $datasetId);
33  return $this->metastoreService->get('dataset', $cleanId);
34  }, array_keys($results));
35 
36  $facetsTypes = $this->getFacetsTypes($params);
37 
38  $facets = $this->getAllFacetsWithZeroCounts($facetsTypes);
39 
40  $facetsKeys = array_reduce($datasets,
41  function ($facetsKeys, $dataset) use ($facetsTypes) {
42  $datasetFacetsKeys = $this->getDatasetFacetsKeys($dataset, $facetsTypes);
43  return array_merge($facetsKeys, $datasetFacetsKeys);
44  }, []);
45 
46  foreach ($facetsKeys as $key) {
47  $facets[$key]->total++;
48  }
49 
50  $facets = array_values($facets);
51  return $facets;
52  }
53 
67  private function getDatasetFacetsKeys(string $dataset, array $facetsTypes) {
68  $rooted = new RootedJsonData($dataset);
69 
70  // Map each facetType to an array of facetKeys of that type.
71  $facetsKeysByType = array_map(function ($facetsType) use ($rooted) {
72  $values = $this->getPropertyValuesFromFacetsType($rooted, $facetsType);
73  return array_map(function ($value) use ($facetsType) {
74  return "{$facetsType}:{$value}";
75  }, $values);
76  }, $facetsTypes);
77 
78  return array_reduce($facetsKeysByType, function ($carry, $facetsKeys) {
79  return array_merge($carry, $facetsKeys);
80  }, []);
81  }
82 
97  private function getPropertyValuesFromFacetsType(RootedJsonData $rooted, string $facetsType) {
98  $property = str_replace('__', '.', $facetsType);
99  $property = "$.{$property}";
100 
101  $values = $rooted->{$property};
102 
103  if (!isset($values)) {
104  $values = [];
105  }
106 
107  if (!is_array($values)) {
108  $values = [$values];
109  }
110 
111  return $values;
112  }
113 
123  private function getAllFacetsWithZeroCounts(array $facetsTypes) {
124  $facets = [];
125  foreach ($facetsTypes as $type) {
126 
127  list($schema, $field) = $this->getSchemaAndField($type);
128 
129  $allFacets = $this->metastoreService->getAll($schema);
130 
131  foreach ($allFacets as $facet) {
132 
133  $facet = isset($field) ? $facet->{"$.data." . $field} : $facet->{"$.data"};
134 
135  $facets["{$type}:{$facet}"] = (object) [
136  'type' => $type,
137  'name' => $facet,
138  'total' => 0,
139  ];
140  }
141  }
142 
143  return $facets;
144  }
145 
155  private function getSchemaAndField(string $facetsType): array {
156  $field = NULL;
157  if (preg_match('/(.*)__(.*)/', $facetsType, $matches)) {
158  $schema = $matches[1];
159  $field = $matches[2];
160  }
161  else {
162  $schema = $facetsType;
163  }
164  return [$schema, $field];
165  }
166 
167 }
Drupal\metastore_search\FacetsFromContentTrait
trait FacetsFromContentTrait
Definition: FacetsFromContentTrait.php:13
Drupal\metastore_search