ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
LegacyAutocompleteSearchResult.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
30{
31 final public const int MODE_STOP_ON_MAX_ENTRIES = 1;
32 final public const int MODE_FETCH_ALL = 2;
33 final public const int MAX_RESULT_ENTRIES = 1000;
34
36 private array $handled_recipients = [];
38 private int $max_entries;
40 public array $result = [
41 'items' => [],
42 'hasMoreResults' => false
43 ];
44
45 public function __construct(int $mode)
46 {
47 $this->max_entries = ilSearchSettings::getInstance()->getAutoCompleteLength();
48
49 $this->initMode($mode);
50 }
51
55 private function initMode(int $mode): void
56 {
57 if (!\in_array($mode, [self::MODE_FETCH_ALL, self::MODE_STOP_ON_MAX_ENTRIES], true)) {
58 throw new \InvalidArgumentException('Wrong mode passed!');
59 }
60 $this->mode = $mode;
61 }
62
63 private function isResultAddable(): bool
64 {
65 if ($this->mode === self::MODE_STOP_ON_MAX_ENTRIES &&
66 $this->max_entries >= 0 && \count($this->result['items']) >= $this->max_entries) {
67 return false;
68 }
69
70 if ($this->mode === self::MODE_FETCH_ALL &&
71 \count($this->result['items']) >= self::MAX_RESULT_ENTRIES) {
72 return false;
73 }
74
75 return true;
76 }
77
78 public function markMoreResultsAvailable(): void
79 {
80 $this->result['hasMoreResults'] = true;
81 }
82
83 public function addResult(string $identifier, string $firstname, string $lastname): SearchResultStatus
84 {
85 if (!$this->isResultAddable()) {
86 throw new \DomainException('Search result is not addable!');
87 }
88
89 if ($identifier === '') {
90 return SearchResultStatus::IGNORED;
91 }
92
93 if (!isset($this->handled_recipients[$identifier])) {
94 $recipient = [];
95 $recipient['value'] = $identifier;
96
97 $label = $identifier;
98 if ($firstname && $lastname) {
99 $label .= ' [' . $firstname . ', ' . $lastname . ']';
100 }
101 $recipient['label'] = $label;
102
103 $this->result['items'][] = $recipient;
104 $this->handled_recipients[$identifier] = true;
105
106 if (!$this->isResultAddable()) {
108 }
109 }
110
111 return SearchResultStatus::DUPLICATE;
112 }
113
117 public function getItems(): array
118 {
119 return $this->result;
120 }
121}
@phpstan-type AutoCompleteUserItem array{label: string, value: string} @phpstan-type AutoCompleteResu...
addResult(string $identifier, string $firstname, string $lastname)