*/ class TreeResourceLookup extends ConfigurableService implements ResourceLookup { const SERVICE_ID = 'tao/TreeResourceLookup'; /** * Retrieve Resources in their hierarchy, for the given parameters as format them as tree. * * @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) { $openNodes = []; if (count($selectedUris) > 0) { $openNodes = TreeHelper::getNodesToOpen($selectedUris, $rootClass); } if (!in_array($rootClass->getUri(), $openNodes)) { $openNodes[] = $rootClass->getUri(); } $factory = new GenerisTreeFactory(true, $openNodes, $limit, $offset, $selectedUris, $propertyFilters); $treeData = $factory->buildTree($rootClass); return $this->formatTreeData([$treeData]); } public function getClasses(\core_kernel_classes_Class $rootClass, array $selectedUris = [], array $propertyFilters = [], $offset = 0, $limit = 30) { $openNodes = []; if (count($selectedUris) > 0) { $openNodes = TreeHelper::getNodesToOpen($selectedUris, $rootClass); } if (!in_array($rootClass->getUri(), $openNodes)) { $openNodes[] = $rootClass->getUri(); } $factory = new GenerisTreeFactory(false, $openNodes, $limit, $offset, $selectedUris, $propertyFilters); $treeData = $factory->buildTree($rootClass); return $this->formatTreeData([$treeData]); } /** * Reformat the the tree : state and count * Add the resource's categories * @param array $treeData * @return array the formated data */ private function formatTreeData(array $treeData) { return array_map(function ($data) { $formated = [ 'label' => $data['data'], 'type' => $data['type'], 'uri' => $data['attributes']['data-uri'], 'classUri' => $data['attributes']['data-classUri'], 'signature' => $data['attributes']['data-signature'], 'state' => isset($data['state']) ? $data['state'] : false, 'count' => isset($data['count']) ? $data['count'] : 0 ]; if (isset($data['children'])) { $formated['children'] = $this->formatTreeData($data['children']); } return $formated; }, $treeData); } }