ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilADTMultiEnum.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21abstract class ilADTMultiEnum extends ilADT
22{
23 protected ?array $values;
24
25 public function getType(): string
26 {
27 return "MultiEnum";
28 }
29
30 // definition
31
32 protected function isValidDefinition(ilADTDefinition $a_def): bool
33 {
34 return $a_def instanceof ilADTMultiEnumDefinition;
35 }
36
37 public function reset(): void
38 {
39 parent::reset();
40 $this->values = null;
41 }
42
43 // properties
44
49 abstract protected function handleSelectionValue($a_value);
50
51 public function addSelection(int $value_index): void
52 {
53 if (!$this->isValidOption($value_index)) {
54 return;
55 }
56 $this->values[] = $value_index;
57 }
58
59 public function setSelections(?array $a_values = null): void
60 {
61 if ($a_values === null) {
62 return;
63 }
64 $checked_values = [];
65 foreach ($a_values as $value_index) {
66 $clean_value = $this->handleSelectionValue($value_index);
67 if ($this->isValidOption($clean_value)) {
68 $checked_values[] = (int) $clean_value;
69 }
70 }
71 $this->values = count($checked_values) ? $checked_values : null;
72 }
73
74 public function getSelections(): ?array
75 {
76 return $this->values;
77 }
78
83 public function isValidOption($a_value): bool
84 {
85 $a_value = $this->handleSelectionValue($a_value);
86 return array_key_exists($a_value, $this->getDefinition()->getOptions());
87 }
88
89 // comparison
90
91 public function equals(ilADT $a_adt): ?bool
92 {
93 if ($this->getDefinition()->isComparableTo($a_adt)) {
94 return ($this->getCheckSum() === $a_adt->getCheckSum());
95 }
96 return null;
97 }
98
99 public function isLarger(ilADT $a_adt): ?bool
100 {
101 return null;
102 }
103
104 public function isSmaller(ilADT $a_adt): ?bool
105 {
106 return null;
107 }
108
109 // null
110
111 public function isNull(): bool
112 {
113 return $this->getSelections() === null;
114 }
115
116 public function getCheckSum(): ?string
117 {
118 if (!$this->isNull()) {
119 $current = $this->getSelections();
120 sort($current);
121 return md5(implode(",", $current));
122 }
123 return null;
124 }
125
126 // stdClass
127
128 public function exportStdClass(): ?stdClass
129 {
130 if (!$this->isNull()) {
131 $obj = new stdClass();
132 $obj->value = (array) $this->getSelections();
133 return $obj;
134 }
135 return null;
136 }
137
138 public function importStdClass(?stdClass $a_std): void
139 {
140 if (is_object($a_std)) {
141 $this->setSelections($a_std->value);
142 }
143 }
144}
ADT definition base class.
importStdClass(?stdClass $a_std)
Import value from stdClass.
isSmaller(ilADT $a_adt)
Check if given ADT is smaller than self.
isValidDefinition(ilADTDefinition $a_def)
Check if definition is valid for ADT.
getType()
Get type (from class/instance)
reset()
Init property defaults.
addSelection(int $value_index)
setSelections(?array $a_values=null)
handleSelectionValue($a_value)
equals(ilADT $a_adt)
Check if given ADT equals self.
getCheckSum()
Get unique checksum.
isNull()
Is currently null.
isLarger(ilADT $a_adt)
Check if given ADT is larger than self.
exportStdClass()
Export value as stdClass.
ADT base class.
Definition: class.ilADT.php:26
getCheckSum()
Get unique checksum.
getDefinition()
Get definition.