ILIAS  release_8 Revision v8.23
class.ilADT.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
11 abstract 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) {
252  case self::ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED:
253  return $this->lng->txt("msg_input_is_required");
254 
255  case self::ADT_VALIDATION_ERROR_MAX_LENGTH:
256  return $this->lng->txt("adt_error_max_length");
257 
258  case self::ADT_VALIDATION_ERROR_MAX_SIZE:
259  return $this->lng->txt("adt_error_max_size");
260 
261  case self::ADT_VALIDATION_ERROR_MIN:
262  return $this->lng->txt("form_msg_value_too_low");
263 
264  case self::ADT_VALIDATION_ERROR_MAX:
265  return $this->lng->txt("form_msg_value_too_high");
266 
267  // :TODO: currently not used - see ilDateTimeInputGUI
268  case self::ADT_VALIDATION_DATE:
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 }
getType()
Get type (from class/instance)
Definition: class.ilADT.php:50
equals(ilADT $a_adt)
Check if given ADT equals self.
addValidationError(string $a_error_code)
const ADT_VALIDATION_ERROR_MAX_SIZE
Definition: class.ilADT.php:25
ilLanguage $lng
Definition: class.ilADT.php:15
const ADT_VALIDATION_ERROR_INVALID_NODE
Definition: class.ilADT.php:35
isSmaller(ilADT $a_adt)
Check if given ADT is smaller than self.
const ADT_VALIDATION_ERROR_MIN
Definition: class.ilADT.php:28
isLarger(ilADT $a_adt)
Check if given ADT is larger than self.
isLargerOrEqual(ilADT $a_adt)
const ADT_VALIDATION_ERROR_MAX_LENGTH
Definition: class.ilADT.php:22
isInbetweenOrEqual(ilADT $a_adt_from, ilADT $a_adt_to)
Check if self is inbetween given ADTs (inclusive)
isSmallerOrEqual(ilADT $a_adt)
Check if given ADT is smaller or equal than self.
ADT base class.
Definition: class.ilADT.php:11
exportStdClass()
Export value as stdClass.
global $DIC
Definition: feed.php:28
isValidDefinition(ilADTDefinition $a_def)
Check if definition is valid for ADT.
const ADT_VALIDATION_ERROR_MAX
Definition: class.ilADT.php:29
reset()
Init property defaults.
Definition: class.ilADT.php:58
__construct(ilADTDefinition $a_def)
Definition: class.ilADT.php:37
importStdClass(?stdClass $a_std)
Import value from stdClass.
getValidationErrors()
Get all validation error codes.
isInbetween(ilADT $a_adt_from, ilADT $a_adt_to)
Check if self is inbetween given ADTs (exclusive)
array $validation_errors
Definition: class.ilADT.php:17
isNull()
Is currently null.
translateErrorCode(string $a_code)
Translate error-code to human-readable message.
getCopyOfDefinition()
Get copy of definition.
getCheckSum()
Get unique checksum.
const ADT_VALIDATION_ERROR_NULL_NOT_ALLOWED
Definition: class.ilADT.php:19
setDefinition(ilADTDefinition $a_def)
Set definition.
Definition: class.ilADT.php:79
ADT definition base class.
const ADT_VALIDATION_DATE
Definition: class.ilADT.php:32
getDefinition()
Get definition.
Definition: class.ilADT.php:92
ilADTDefinition $definition
Definition: class.ilADT.php:13