ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADT.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
25abstract class ilADT
26{
28
29 protected ilLanguage $lng;
30
31 protected array $validation_errors = []; // [array]
32
34
35 // text-based
36 public const ADT_VALIDATION_ERROR_MAX_LENGTH = "adt2";
37
38 // multi
39 public const ADT_VALIDATION_ERROR_MAX_SIZE = "adt3";
40
41 // number-based
42 public const ADT_VALIDATION_ERROR_MIN = "adt4";
43 public const ADT_VALIDATION_ERROR_MAX = "adt5";
44
45 // date-based
46 public const ADT_VALIDATION_DATE = "adt6";
47
48 // invalid target node for internal link
49 public const ADT_VALIDATION_ERROR_INVALID_NODE = 'adt7';
50
51 public function __construct(ilADTDefinition $a_def)
52 {
53 global $DIC;
54 $this->setDefinition($a_def);
55 $this->reset();
56
57 $this->lng = $DIC->language();
58 }
59
64 public function getType(): string
65 {
66 return $this->getDefinition()->getType();
67 }
68
72 public function reset(): void
73 {
74 }
75
76
77 //
78 // definition
79 //
80
86 abstract protected function isValidDefinition(ilADTDefinition $a_def): bool;
87
93 protected function setDefinition(ilADTDefinition $a_def): void
94 {
95 if ($this->isValidDefinition($a_def)) {
96 $this->definition = clone $a_def;
97 } else {
98 throw new ilException("ilADT invalid definition");
99 }
100 }
101
106 protected function getDefinition(): ilADTDefinition
107 {
108 return $this->definition;
109 }
110
116 {
117 return (clone $this->definition);
118 }
119
120
121 //
122 // comparison
123 //
124
130 abstract public function equals(ilADT $a_adt): ?bool;
131
137 abstract public function isLarger(ilADT $a_adt): ?bool;
138
139 public function isLargerOrEqual(ilADT $a_adt): ?bool
140 {
141 if (!$this->getDefinition()->isComparableTo($a_adt)) {
142 return null;
143 }
144 if ($this->equals($a_adt) === null || $this->isLarger($a_adt) === null) {
145 return null;
146 }
147 return $this->equals($a_adt) || $this->isLarger($a_adt);
148 }
149
155 abstract public function isSmaller(ilADT $a_adt): ?bool;
156
162 public function isSmallerOrEqual(ilADT $a_adt): ?bool
163 {
164 if (!$this->getDefinition()->isComparableTo($a_adt)) {
165 return null;
166 }
167 if ($this->equals($a_adt) === null || $this->isSmaller($a_adt) === null) {
168 return null;
169 }
170 return $this->equals($a_adt) || $this->isSmaller($a_adt);
171 }
172
179 public function isInbetween(ilADT $a_adt_from, ilADT $a_adt_to): ?bool
180 {
181 if (
182 !$this->getDefinition()->isComparableTo($a_adt_from) ||
183 !$this->getDefinition()->isComparableTo($a_adt_to)
184 ) {
185 return null;
186 }
187 if ($this->isLarger($a_adt_from) === null || $this->isSmaller($a_adt_to) === null) {
188 return null;
189 }
190 return $this->isLarger($a_adt_from) && $this->isSmaller($a_adt_to);
191 }
192
199 public function isInbetweenOrEqual(ilADT $a_adt_from, ilADT $a_adt_to): ?bool
200 {
201 if (
202 !$this->getDefinition()->isComparableTo($a_adt_from) ||
203 !$this->getDefinition()->isComparableTo($a_adt_to)
204 ) {
205 return null;
206 }
207 if (
208 $this->equals($a_adt_from) === null ||
209 $this->equals($a_adt_to) === null ||
210 $this->isInbetween($a_adt_from, $a_adt_to) === null
211 ) {
212 return null;
213 }
214 return
215 $this->equals($a_adt_from) ||
216 $this->equals($a_adt_to) ||
217 $this->isInbetween($a_adt_from, $a_adt_to);
218 }
219
224 abstract public function isNull(): bool;
225
226 public function isValid(): bool
227 {
228 $this->validation_errors = [];
229
230 if (!$this->getDefinition()->isNullAllowed() && $this->isNull()) {
231 $this->addValidationError(self::ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED);
232 return false;
233 }
234 return true;
235 }
236
237 protected function addValidationError(string $a_error_code): void
238 {
239 $this->validation_errors[] = $a_error_code;
240 }
241
247 public function getValidationErrors(): array
248 {
249 if (
250 is_array($this->validation_errors) &&
251 count($this->validation_errors)) {
252 return array_unique($this->validation_errors);
253 }
254 return [];
255 }
256
263 public function translateErrorCode(string $a_code): string
264 {
265 switch ($a_code) {
267 return $this->lng->txt("msg_input_is_required");
268
270 return $this->lng->txt("adt_error_max_length");
271
273 return $this->lng->txt("adt_error_max_size");
274
276 return $this->lng->txt("form_msg_value_too_low");
277
279 return $this->lng->txt("form_msg_value_too_high");
280
281 // :TODO: currently not used - see ilDateTimeInputGUI
283 return $this->lng->txt("exc_date_not_valid");
284 }
285 throw new Exception("ADT unknown error code");
286 }
287
292 abstract public function getCheckSum(): ?string;
293
298 abstract public function exportStdClass(): ?stdClass;
299
304 abstract public function importStdClass(?stdClass $a_std): void;
305}
ADT definition base class.
ADT base class.
Definition: class.ilADT.php:26
array $validation_errors
Definition: class.ilADT.php:31
ilADTDefinition $definition
Definition: class.ilADT.php:27
ilLanguage $lng
Definition: class.ilADT.php:29
const ADT_VALIDATION_ERROR_MAX_SIZE
Definition: class.ilADT.php:39
equals(ilADT $a_adt)
Check if given ADT equals self.
isSmaller(ilADT $a_adt)
Check if given ADT is smaller than self.
isSmallerOrEqual(ilADT $a_adt)
Check if given ADT is smaller or equal than self.
const ADT_VALIDATION_ERROR_MAX_LENGTH
Definition: class.ilADT.php:36
isNull()
Is currently null.
const ADT_VALIDATION_ERROR_MAX
Definition: class.ilADT.php:43
isLargerOrEqual(ilADT $a_adt)
addValidationError(string $a_error_code)
getType()
Get type (from class/instance)
Definition: class.ilADT.php:64
getCopyOfDefinition()
Get copy of definition.
exportStdClass()
Export value as stdClass.
__construct(ilADTDefinition $a_def)
Definition: class.ilADT.php:51
const ADT_VALIDATION_ERROR_INVALID_NODE
Definition: class.ilADT.php:49
reset()
Init property defaults.
Definition: class.ilADT.php:72
translateErrorCode(string $a_code)
Translate error-code to human-readable message.
setDefinition(ilADTDefinition $a_def)
Set definition.
Definition: class.ilADT.php:93
importStdClass(?stdClass $a_std)
Import value from stdClass.
getValidationErrors()
Get all validation error codes.
getCheckSum()
Get unique checksum.
getDefinition()
Get definition.
isValidDefinition(ilADTDefinition $a_def)
Check if definition is valid for ADT.
isInbetween(ilADT $a_adt_from, ilADT $a_adt_to)
Check if self is inbetween given ADTs (exclusive)
const ADT_VALIDATION_ERROR_MIN
Definition: class.ilADT.php:42
const ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED
Definition: class.ilADT.php:33
const ADT_VALIDATION_DATE
Definition: class.ilADT.php:46
isLarger(ilADT $a_adt)
Check if given ADT is larger than self.
isInbetweenOrEqual(ilADT $a_adt_from, ilADT $a_adt_to)
Check if self is inbetween given ADTs (inclusive)
Base class for ILIAS Exception handling.
language handling
global $DIC
Definition: shib_login.php:26