*/ class ListResourceLookup extends ConfigurableService implements ResourceLookup { use OntologyAwareTrait; const SERVICE_ID = 'tao/ListResourceLookup'; /** * Retrieve Resources for the given parameters as a list * * @param \core_kernel_classes_Class $rootClass the resources class * @param array $propertyFilters propUri/propValue to search resources * @param string[] $selectedUris the resources to open * @param int $offset for paging * @param int $limit for paging * @return array the resources */ public function getResources(\core_kernel_classes_Class $rootClass, array $selectedUris = [], array $propertyFilters = [], $offset = 0, $limit = 30) { // Searching by label parameter will utilize fulltext search if (count($propertyFilters) == 1 && isset($propertyFilters[OntologyRdfs::RDFS_LABEL])) { $searchString = current($propertyFilters); return $this->searchByString($searchString, $rootClass, $offset, $limit); } else { return $this->searchByProperties($propertyFilters, $rootClass, $offset, $limit); } } /** * Search using an advanced search string * @param string $searchString * @param \core_kernel_classes_Class $rootClass * @param int $offset * @param int $limit * @return array */ private function searchByString($searchString, $rootClass, $offset, $limit) { /** @var Search $searchService */ $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID); /** @var ResultSet $result */ $result = $searchService->query($searchString, $rootClass, $offset, $limit); $count = $result->getTotalCount(); return $this->format($result, $count, $offset, $limit); } /** * Search using properties * @param string $searchString * @param \core_kernel_classes_Class $rootClass * @param int $offset * @param int $limit * @return array */ private function searchByProperties($propertyFilters, $rootClass, $offset, $limit) { // for searching by properties will be used RDF search $options = [ 'recursive' => true, 'like' => true, 'limit' => $limit, 'offset' => $offset ]; $count = $rootClass->countInstances($propertyFilters, $options); $resources = $rootClass->searchInstances($propertyFilters, $options); return $this->format($resources, $count, $offset, $limit); } /** * Format the results according to the needs of ListLookup * @param array $result * @param int $count * @param int $offset * @param int $limit * @return array */ private function format($result, $count, $offset, $limit) { $nodes = []; foreach ($result as $item) { $resource = $this->getResource($item); $data = $this->getResourceData($resource); if ($data) { $nodes[] = $data; } } return [ 'total' => $count, 'offset' => $offset, 'limit' => $limit, 'nodes' => $nodes ]; } /** * Preparing resource to be used in the ListLookup * @param $resource * @return array|bool */ private function getResourceData($resource) { $data = false; if (!is_null($resource) && $resource->exists()) { $resourceTypes = array_keys($resource->getTypes()); $data = [ 'uri' => $resource->getUri(), 'classUri' => $resourceTypes[0], 'label' => $resource->getLabel(), 'type' => 'instance' ]; } return $data; } public function getClasses(\core_kernel_classes_Class $rootClass, array $selectedUris = [], array $propertyFilters = [], $offset = 0, $limit = 30) { return []; } }