ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADTText.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21class ilADTText extends ilADT
22{
23 protected ?string $value;
24
25 // definition
26
27 protected function isValidDefinition(ilADTDefinition $a_def): bool
28 {
29 return $a_def instanceof ilADTTextDefinition;
30 }
31
32 public function reset(): void
33 {
34 parent::reset();
35 $this->value = null;
36 }
37
38 // properties
39
40 public function setText(?string $a_value = null): void
41 {
42 if ($a_value !== null) {
43 $a_value = trim($a_value);
44 }
45 $this->value = $a_value;
46 }
47
48 public function getText(): ?string
49 {
50 return $this->value;
51 }
52
53 public function getLength(): int
54 {
55 if (function_exists("mb_strlen")) {
56 return mb_strlen((string) $this->getText(), "UTF-8");
57 } else {
58 return strlen((string) $this->getText());
59 }
60 }
61
62 // comparison
63
64 public function equals(ilADT $a_adt): ?bool
65 {
66 if ($this->getDefinition()->isComparableTo($a_adt)) {
67 return !strcmp($this->getText(), $a_adt->getText());
68 }
69 return null;
70 }
71
72 public function isLarger(ilADT $a_adt): ?bool
73 {
74 return null;
75 }
76
77 public function isSmaller(ilADT $a_adt): ?bool
78 {
79 return null;
80 }
81
82 // null
83
84 public function isNull(): bool
85 {
86 return !$this->getLength();
87 }
88
89 // validation
90
91 public function isValid(): bool
92 {
93 $valid = parent::isValid();
94 if (!$this->isNull()) {
95 $max = $this->getDefinition()->getMaxLength();
96 if ($max && $max < $this->getLength()) {
97 $valid = false;
98 $this->addValidationError(self::ADT_VALIDATION_ERROR_MAX_LENGTH);
99 }
100 }
101 return $valid;
102 }
103
104 public function getCheckSum(): ?string
105 {
106 if (!$this->isNull()) {
107 return md5($this->getText());
108 }
109 return null;
110 }
111
112 public function exportStdClass(): ?stdClass
113 {
114 if (!$this->isNull()) {
115 $obj = new stdClass();
116 $obj->value = $this->getText();
117 return $obj;
118 }
119 return null;
120 }
121
122 public function importStdClass(?stdClass $a_std): void
123 {
124 if (is_object($a_std)) {
125 $this->setText($a_std->value);
126 }
127 }
128}
ADT definition base class.
reset()
Init property defaults.
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:26
addValidationError(string $a_error_code)
getDefinition()
Get definition.
$valid