ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Sequence.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\UI\Implementation\Component\ComponentHelper;
25use Psr\Http\Message\ServerRequestInterface;
28use ILIAS\Data\Factory as DataFactory;
29use ILIAS\Refinery\Factory as Refinery;
34
36{
37 use ComponentHelper;
38
39 private const PARAM_POSITION = 'p';
40 public const STORAGE_ID_PREFIX = self::class . '_';
41
42 protected ?ServerRequestInterface $request = null;
43 protected ?URLBuilder $url_builder = null;
45 protected ?ViewControlContainer $viewcontrols = null;
46 protected ?array $actions = null;
47 protected int $position = 0;
48 protected ?string $id = null;
49
50 public function __construct(
51 protected DataFactory $data_factory,
52 protected Refinery $refinery,
53 protected Storage $storage,
54 protected ISequence\SegmentRetrieval $segment_retrieval
55 ) {
56 }
57
58 public function getSegmentRetrieval(): ISequence\SegmentRetrieval
59 {
60 return $this->segment_retrieval;
61 }
62
63 public function getCurrentPosition(): int
64 {
65 return $this->position;
66 }
67
68 public function withCurrentPosition(int $position): static
69 {
70 $clone = clone $this;
71 $clone->position = $position;
72 return $clone;
73 }
74
75 public function withViewControls(ViewControlContainer $viewcontrols): static
76 {
77 $clone = clone $this;
78 $clone->viewcontrols = $viewcontrols;
79 return $clone;
80 }
81
82 public function getViewControls(): ?ViewControlContainer
83 {
85 }
86
87 public function withActions(...$actions): static
88 {
89 $clone = clone $this;
90 $clone->actions = $actions;
91 return $clone;
92 }
93
94 public function getActions(): ?array
95 {
96 return $this->actions;
97 }
98
99 protected function checkRequest(): void
100 {
101 if (! $this->request) {
102 throw new \LogicException('no request was set on the sequence');
103 }
104 }
105
106 public function withRequest(ServerRequestInterface $request): static
107 {
108 $clone = clone $this;
109 $clone->request = $request;
110 $clone->initFromRequest();
111 return $clone;
112 }
113
114 public function getRequest(): ServerRequestInterface
115 {
116 $this->checkRequest();
117 return $this->request;
118 }
119
120 protected function initFromRequest(): void
121 {
122 $base_uri = $this->data_factory->uri($this->request->getUri()->__toString());
123 $namespace = ['sequence_' . $this->getId() ?? ''];
124 $url_builder = new URLBuilder($base_uri);
125 list(
126 $this->url_builder,
127 $this->token_position
130 self::PARAM_POSITION
131 );
132
133 $query = new \ILIAS\HTTP\Wrapper\ArrayBasedRequestWrapper($this->request->getQueryParams());
134 $this->position = $query->retrieve(
135 $this->token_position->getName(),
136 $this->refinery->byTrying([
137 $this->refinery->kindlyTo()->int(),
138 $this->refinery->always(0)
139 ])
140 );
141
142 if ($this->viewcontrols) {
143 $this->viewcontrols = $this->applyValuesToViewcontrols(
144 $this->viewcontrols,
145 $this->request
146 );
147 }
148 }
149
150 public function getNext(int $direction): URI
151 {
152 $this->checkRequest();
153 return $this->url_builder
154 ->withParameter($this->token_position, (string) ($this->position + $direction))
155 ->buildURI();
156 }
157
158 protected function getStorageData(): ?array
159 {
160 if (null !== ($storage_id = $this->getStorageId())) {
161 return $this->storage[$storage_id] ?? null;
162 }
163 return null;
164 }
165
166 protected function setStorageData(array $data): void
167 {
168 if (null !== ($storage_id = $this->getStorageId())) {
169 $this->storage[$storage_id] = $data;
170 }
171 }
172
173 protected function getStorageId(): ?string
174 {
175 if (null !== ($id = $this->getId())) {
176 return static::STORAGE_ID_PREFIX . $id;
177 }
178 return null;
179 }
180
181 public function withId(string $id): static
182 {
183 $clone = clone $this;
184 $clone->id = $id;
185 return $clone;
186 }
187
188 protected function getId(): ?string
189 {
190 return $this->id;
191 }
192
193 protected function applyValuesToViewcontrols(
194 ViewControlContainer $view_controls,
195 ServerRequestInterface $request
196 ): ViewControlContainer {
197 $stored_values = new ArrayInputData($this->getStorageData() ?? []);
198 $view_controls = $view_controls
199 ->withStoredInput($stored_values)
200 ->withRequest($request);
201 $this->setStorageData($view_controls->getComponentInternalValues());
202 return $view_controls;
203 }
204
205}
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
applyValuesToViewcontrols(ViewControlContainer $view_controls, ServerRequestInterface $request)
Definition: Sequence.php:193
withRequest(ServerRequestInterface $request)
Rendering the sequence must be done using the current request: it (the request) will carry parameters...
Definition: Sequence.php:106
withId(string $id)
The Sequence comes with a storage to keep ViewControl-settings throughout requests.
Definition: Sequence.php:181
__construct(protected DataFactory $data_factory, protected Refinery $refinery, protected Storage $storage, protected ISequence\SegmentRetrieval $segment_retrieval)
Definition: Sequence.php:50
withViewControls(ViewControlContainer $viewcontrols)
You may add view controls to the sequences's player that alter the way, order or focus in which the s...
Definition: Sequence.php:75
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
if($err=$client->getError()) $namespace
This describes a View Control Container.
Definition: ViewControl.php:30
Storage is simple key/value store without further schema definition.
Definition: Storage.php:30