ILIAS  release_8 Revision v8.24
class.ilADTText.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5class ilADTText extends ilADT
6{
7 protected ?string $value;
8
9 // definition
10
11 protected function isValidDefinition(ilADTDefinition $a_def): bool
12 {
13 return $a_def instanceof ilADTTextDefinition;
14 }
15
16 public function reset(): void
17 {
18 parent::reset();
19 $this->value = null;
20 }
21
22 // properties
23
24 public function setText(?string $a_value = null): void
25 {
26 if ($a_value !== null) {
27 $a_value = trim($a_value);
28 }
29 $this->value = $a_value;
30 }
31
32 public function getText(): ?string
33 {
34 return $this->value;
35 }
36
37 public function getLength(): int
38 {
39 if (function_exists("mb_strlen")) {
40 return mb_strlen((string) $this->getText(), "UTF-8");
41 } else {
42 return strlen((string) $this->getText());
43 }
44 }
45
46 // comparison
47
48 public function equals(ilADT $a_adt): ?bool
49 {
50 if ($this->getDefinition()->isComparableTo($a_adt)) {
51 return !strcmp($this->getText(), $a_adt->getText());
52 }
53 return null;
54 }
55
56 public function isLarger(ilADT $a_adt): ?bool
57 {
58 return null;
59 }
60
61 public function isSmaller(ilADT $a_adt): ?bool
62 {
63 return null;
64 }
65
66 // null
67
68 public function isNull(): bool
69 {
70 return !$this->getLength();
71 }
72
73 // validation
74
75 public function isValid(): bool
76 {
77 $valid = parent::isValid();
78 if (!$this->isNull()) {
79 $max = $this->getDefinition()->getMaxLength();
80 if ($max && $max < $this->getLength()) {
81 $valid = false;
82 $this->addValidationError(self::ADT_VALIDATION_ERROR_MAX_LENGTH);
83 }
84 }
85 return $valid;
86 }
87
88 public function getCheckSum(): ?string
89 {
90 if (!$this->isNull()) {
91 return md5($this->getText());
92 }
93 return null;
94 }
95
96 public function exportStdClass(): ?stdClass
97 {
98 if (!$this->isNull()) {
99 $obj = new stdClass();
100 $obj->value = $this->getText();
101 return $obj;
102 }
103 return null;
104 }
105
106 public function importStdClass(?stdClass $a_std): void
107 {
108 if (is_object($a_std)) {
109 $this->setText($a_std->value);
110 }
111 }
112}
ADT definition base class.
reset()
Init property defaults.
string $value
isNull()
Is currently null.
getCheckSum()
Get unique checksum.
isLarger(ilADT $a_adt)
Check if given ADT is larger than self.
equals(ilADT $a_adt)
Check if given ADT equals self.
setText(?string $a_value=null)
importStdClass(?stdClass $a_std)
Import value from stdClass.
isValidDefinition(ilADTDefinition $a_def)
Check if definition is valid for ADT.
isSmaller(ilADT $a_adt)
Check if given ADT is smaller than self.
exportStdClass()
Export value as stdClass.
ADT base class.
Definition: class.ilADT.php:12
addValidationError(string $a_error_code)
getDefinition()
Get definition.
Definition: class.ilADT.php:92
$valid