ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ProvideDocument.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
43use ilObjUser;
44
46{
47 public const CRITERION_ALREADY_EXISTS = 'Criterion already exists.';
48 public const CRITERION_WOULD_NEVER_MATCH = 'Criterion would never match.';
49
53 public function __construct(
54 private readonly string $id,
55 private readonly DocumentRepository $document_repository,
56 private readonly SelectionMap $conditions,
57 private readonly array $content_as_component,
58 private readonly Container $container,
59 ) {
60 }
61
62 public function table(object $gui, ?EditLinks $edit_links = null): DocumentTable
63 {
64 return new DocumentTable(
65 fn($criterion) => $this->toCondition($criterion)->asComponent(),
66 $this->document_repository,
67 new UI(
68 $this->id,
69 $this->container->ui(),
70 $this->container->language()
71 ),
72 new DocumentModal($this->container->ui(), $this->contentAsComponent(...)),
73 $gui,
74 $edit_links,
75 $this->container->http()->request(),
76 new Factory(),
77 $this->container->ctrl(),
78 $this->container->ui()->renderer(),
79 $this->container->user()
80 );
81 }
82
83 public function chooseDocumentFor(ilObjUser $user): Result
84 {
85 return $this->find(
86 fn(Document $document): bool => $this->documentMatches($document, $user),
87 $this->document_repository->all()
88 );
89 }
90
91 public function documentMatches(Document $document, ilObjUser $user): bool
92 {
93 return $this->all(
94 fn($c) => $this->toCondition($c->content())->eval($user),
95 $document->criteria()
96 );
97 }
98
99 public function repository(): DocumentRepository
100 {
101 return $this->document_repository;
102 }
103
104 public function hash(): string
105 {
106 return bin2hex(openssl_random_pseudo_bytes((int) floor(255 / 2)));
107 }
108
109 public function toCondition(CriterionContent $content): Condition
110 {
111 return $this->conditions->choices()[$content->type()]->withCriterion($content);
112 }
113
117 public function conditionGroups(?CriterionContent $criterion = null): SelectionMap
118 {
119 $selected = $criterion ? [
120 $criterion->type() => $criterion->arguments()
121 ] : [];
122
123 $choices = $this->conditions->choices();
124 $groups = array_combine(array_keys($choices), array_map(
125 fn(ConditionDefinition $condition, string $key) => $condition->formGroup($selected[$key] ?? []),
126 array_values($choices),
127 array_keys($choices)
128 ));
129
130 return new SelectionMap($groups, $this->conditions->defaultSelection());
131 }
132
136 public function validateCriteriaContent(array $criteria, CriterionContent $content): Result
137 {
138 return array_reduce(
139 $criteria,
140 $this->validateAgainst(...),
141 new Ok($content)
142 );
143 }
144
146 {
147 return $this->content_as_component[$content->type()]($content);
148 }
149
151 {
152 return $b->then(function (CriterionContent $b) use ($a) {
153 $a = $a->content();
154 if ($a->equals($b)) {
155 return new Error(self::CRITERION_ALREADY_EXISTS);
156 }
157 $a = $this->toCondition($a);
158 if ($a->knownToNeverMatchWith($this->toCondition($b))) {
159 return new Error(self::CRITERION_WOULD_NEVER_MATCH);
160 }
161 return new Ok($b);
162 });
163 }
164
171 private function find(Closure $predicate, array $array): Result
172 {
173 foreach ($array as $x) {
174 if ($predicate($x)) {
175 return new Ok($x);
176 }
177 }
178
179 return new Error('Not found.');
180 }
181
187 private function any(Closure $predicate, array $array): bool
188 {
189 return $this->find($predicate, $array)->isOk();
190 }
191
197 private function all(Closure $predicate, array $array): bool
198 {
199 return !$this->any(static fn($x) => !$predicate($x), $array);
200 }
201}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds data types.
Definition: Factory.php:36
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
conditionGroups(?CriterionContent $criterion=null)
validateCriteriaContent(array $criteria, CriterionContent $content)
all(Closure $predicate, array $array)
@template A
find(Closure $predicate, array $array)
@template A
documentMatches(Document $document, ilObjUser $user)
__construct(private readonly string $id, private readonly DocumentRepository $document_repository, private readonly SelectionMap $conditions, private readonly array $content_as_component, private readonly Container $container,)
any(Closure $predicate, array $array)
@template A
table(object $gui, ?EditLinks $edit_links=null)
User class.
$c
Definition: deliver.php:25
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
A component is the most general form of an entity in the UI.
Definition: Component.php:28
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
if(!file_exists('../ilias.ini.php'))
$container
@noRector
Definition: wac.php:37