ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
class.EndpointGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\User\Search;
22
23use ILIAS\User\Profile\Fields\ConfigurationRepository as FieldConfigurationRepository;
24use ILIAS\User\Profile\DataRepository as ProfileDataRepository;
25use ILIAS\User\Settings\DataRepository as SettingsDataRepository;
26use ILIAS\Data\Factory as DataFactory;
28use ILIAS\HTTP\Services as HttpService;
29use ILIAS\Refinery\Factory as Refinery;
31
33{
34 private const array NAMESPACE = ['u', 's'];
35 private const string SEARCH_TERM_TOKEN = 't';
36 private const int SUGGESTIONS_START_AFTER = 3;
37
38 public function __construct(
39 private readonly FieldConfigurationRepository $field_configuration_repository,
40 private readonly ProfileDataRepository $profile_data_repository,
41 private readonly SettingsDataRepository $settings_data_repository,
42 private readonly \ilObjUser $current_user,
43 private readonly HttpService $http,
44 private readonly Refinery $refinery,
45 private readonly \ilCtrl $ctrl,
46 private readonly DataFactory $data_factory,
47 private readonly EndpointConfigurator $endpoint_configurator
48 ) {
49 }
50
54 public function acquireBuilderAndToken(): array
55 {
56 return (new URLBuilder(
57 $this->data_factory->uri(
58 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
59 array_merge(
60 $this->endpoint_configurator->getParentClassPath(),
61 [self::class]
62 )
63 )
64 )
65 ))->acquireParameter(self::NAMESPACE, self::SEARCH_TERM_TOKEN);
66 }
67
68 public function getSuggestionsStartAfter(): int
69 {
71 }
72
73 public function executeCommand(): void
74 {
75 $this->http->saveResponse(
76 $this->http->response()->withBody(
77 Streams::ofString($this->buildResponse())
78 )
79 );
80 $this->http->sendResponse();
81 $this->http->close();
82 }
83
84 private function buildResponse(): string
85 {
86 if ($this->current_user->getId() === ANONYMOUS_USER_ID) {
87 return '';
88 }
89
91 [, $token] = $this->acquireBuilderAndToken();
92
93 $autocomplete_query = $this->http->wrapper()->query()->retrieve(
94 $token->getName(),
95 $this->refinery->byTrying([
96 $this->refinery->custom()->transformation(
97 $this->buildQueryStringTransformation()
98 ),
99 $this->refinery->always(null)
100 ])
101 );
102
103 $response = array_map(
104 static fn(AutocompleteItem $v) => $v->getTagArray(),
105 array_merge(
106 $this->endpoint_configurator->getAdditionalAnswerElements(
107 $this->current_user,
108 $autocomplete_query
109 ),
110 $this->profile_data_repository->searchUsers(
111 $this->settings_data_repository,
112 $this->field_configuration_repository,
113 $autocomplete_query
114 )
115 )
116 );
117
118 usort(
119 $response,
120 fn(array $a, array $b): int => $a['display'] <=> $b['display']
121 );
122
123 return json_encode($response);
124 }
125
126 private function buildQueryStringTransformation(): \Closure
127 {
128 return function ($parameter): AutocompleteQuery {
129 return new AutocompleteQuery(
131 $this->refinery->kindlyTo()->string()->transform($parameter)
132 );
133 };
134 }
135}
Builds data types.
Definition: Factory.php:36
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
Class Services.
Definition: Services.php:38
This class provides some pre-processing for search terms provided by a user when searching for users.
__construct(private readonly FieldConfigurationRepository $field_configuration_repository, private readonly ProfileDataRepository $profile_data_repository, private readonly SettingsDataRepository $settings_data_repository, private readonly \ilObjUser $current_user, private readonly HttpService $http, private readonly Refinery $refinery, private readonly \ilCtrl $ctrl, private readonly DataFactory $data_factory, private readonly EndpointConfigurator $endpoint_configurator)
Class ilCtrl provides processing control methods.
User class.
const ANONYMOUS_USER_ID
Definition: constants.php:27
$http
Definition: deliver.php:30
static http()
Fetches the global http state from ILIAS.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$token
Definition: xapitoken.php:67
$response
Definition: xapitoken.php:90