ILIAS  release_8 Revision v8.24
class.ilADT.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5
11abstract class ilADT
12{
14
15 protected ilLanguage $lng;
16
17 protected array $validation_errors = []; // [array]
18
20
21 // text-based
22 public const ADT_VALIDATION_ERROR_MAX_LENGTH = "adt2";
23
24 // multi
25 public const ADT_VALIDATION_ERROR_MAX_SIZE = "adt3";
26
27 // number-based
28 public const ADT_VALIDATION_ERROR_MIN = "adt4";
29 public const ADT_VALIDATION_ERROR_MAX = "adt5";
30
31 // date-based
32 public const ADT_VALIDATION_DATE = "adt6";
33
34 // invalid target node for internal link
35 public const ADT_VALIDATION_ERROR_INVALID_NODE = 'adt7';
36
37 public function __construct(ilADTDefinition $a_def)
38 {
39 global $DIC;
40 $this->setDefinition($a_def);
41 $this->reset();
42
43 $this->lng = $DIC->language();
44 }
45
50 public function getType(): string
51 {
52 return $this->getDefinition()->getType();
53 }
54
58 public function reset(): void
59 {
60 }
61
62
63 //
64 // definition
65 //
66
72 abstract protected function isValidDefinition(ilADTDefinition $a_def): bool;
73
79 protected function setDefinition(ilADTDefinition $a_def): void
80 {
81 if ($this->isValidDefinition($a_def)) {
82 $this->definition = clone $a_def;
83 } else {
84 throw new ilException("ilADT invalid definition");
85 }
86 }
87
92 protected function getDefinition(): ilADTDefinition
93 {
94 return $this->definition;
95 }
96
102 {
103 return (clone $this->definition);
104 }
105
106
107 //
108 // comparison
109 //
110
116 abstract public function equals(ilADT $a_adt): ?bool;
117
123 abstract public function isLarger(ilADT $a_adt): ?bool;
124
125 public function isLargerOrEqual(ilADT $a_adt): ?bool
126 {
127 if (!$this->getDefinition()->isComparableTo($a_adt)) {
128 return null;
129 }
130 if ($this->equals($a_adt) === null || $this->isLarger($a_adt) === null) {
131 return null;
132 }
133 return $this->equals($a_adt) || $this->isLarger($a_adt);
134 }
135
141 abstract public function isSmaller(ilADT $a_adt): ?bool;
142
148 public function isSmallerOrEqual(ilADT $a_adt): ?bool
149 {
150 if (!$this->getDefinition()->isComparableTo($a_adt)) {
151 return null;
152 }
153 if ($this->equals($a_adt) === null || $this->isSmaller($a_adt) === null) {
154 return null;
155 }
156 return $this->equals($a_adt) || $this->isSmaller($a_adt);
157 }
158
165 public function isInbetween(ilADT $a_adt_from, ilADT $a_adt_to): ?bool
166 {
167 if (
168 !$this->getDefinition()->isComparableTo($a_adt_from) ||
169 !$this->getDefinition()->isComparableTo($a_adt_to)
170 ) {
171 return null;
172 }
173 if ($this->isLarger($a_adt_from) === null || $this->isSmaller($a_adt_to) === null) {
174 return null;
175 }
176 return $this->isLarger($a_adt_from) && $this->isSmaller($a_adt_to);
177 }
178
185 public function isInbetweenOrEqual(ilADT $a_adt_from, ilADT $a_adt_to): ?bool
186 {
187 if (
188 !$this->getDefinition()->isComparableTo($a_adt_from) ||
189 !$this->getDefinition()->isComparableTo($a_adt_to)
190 ) {
191 return null;
192 }
193 if (
194 $this->equals($a_adt_from) === null ||
195 $this->equals($a_adt_to) === null ||
196 $this->isInbetween($a_adt_from, $a_adt_to) === null
197 ) {
198 return null;
199 }
200 return
201 $this->equals($a_adt_from) ||
202 $this->equals($a_adt_to) ||
203 $this->isInbetween($a_adt_from, $a_adt_to);
204 }
205
210 abstract public function isNull(): bool;
211
212 public function isValid(): bool
213 {
214 $this->validation_errors = [];
215
216 if (!$this->getDefinition()->isNullAllowed() && $this->isNull()) {
217 $this->addValidationError(self::ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED);
218 return false;
219 }
220 return true;
221 }
222
223 protected function addValidationError(string $a_error_code): void
224 {
225 $this->validation_errors[] = $a_error_code;
226 }
227
233 public function getValidationErrors(): array
234 {
235 if (
236 is_array($this->validation_errors) &&
237 count($this->validation_errors)) {
238 return array_unique($this->validation_errors);
239 }
240 return [];
241 }
242
249 public function translateErrorCode(string $a_code): string
250 {
251 switch ($a_code) {
253 return $this->lng->txt("msg_input_is_required");
254
256 return $this->lng->txt("adt_error_max_length");
257
259 return $this->lng->txt("adt_error_max_size");
260
262 return $this->lng->txt("form_msg_value_too_low");
263
265 return $this->lng->txt("form_msg_value_too_high");
266
267 // :TODO: currently not used - see ilDateTimeInputGUI
269 return $this->lng->txt("exc_date_not_valid");
270
271 }
272 throw new Exception("ADT unknown error code");
273 }
274
279 abstract public function getCheckSum(): ?string;
280
285 abstract public function exportStdClass(): ?stdClass;
286
291 abstract public function importStdClass(?stdClass $a_std): void;
292}
ADT definition base class.
ADT base class.
Definition: class.ilADT.php:12
array $validation_errors
Definition: class.ilADT.php:17
ilADTDefinition $definition
Definition: class.ilADT.php:13
ilLanguage $lng
Definition: class.ilADT.php:15
const ADT_VALIDATION_ERROR_MAX_SIZE
Definition: class.ilADT.php:25
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:22
isNull()
Is currently null.
const ADT_VALIDATION_ERROR_MAX
Definition: class.ilADT.php:29
isLargerOrEqual(ilADT $a_adt)
addValidationError(string $a_error_code)
getType()
Get type (from class/instance)
Definition: class.ilADT.php:50
getCopyOfDefinition()
Get copy of definition.
exportStdClass()
Export value as stdClass.
__construct(ilADTDefinition $a_def)
Definition: class.ilADT.php:37
const ADT_VALIDATION_ERROR_INVALID_NODE
Definition: class.ilADT.php:35
reset()
Init property defaults.
Definition: class.ilADT.php:58
translateErrorCode(string $a_code)
Translate error-code to human-readable message.
setDefinition(ilADTDefinition $a_def)
Set definition.
Definition: class.ilADT.php:79
importStdClass(?stdClass $a_std)
Import value from stdClass.
getValidationErrors()
Get all validation error codes.
getCheckSum()
Get unique checksum.
getDefinition()
Get definition.
Definition: class.ilADT.php:92
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:28
const ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED
Definition: class.ilADT.php:19
const ADT_VALIDATION_DATE
Definition: class.ilADT.php:32
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
global $DIC
Definition: feed.php:28