ILIAS  release_8 Revision v8.24
class.ilADTMultiEnum.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5abstract class ilADTMultiEnum extends ilADT
6{
7 protected ?array $values;
8
9 public function getType(): string
10 {
11 return "MultiEnum";
12 }
13
14 // definition
15
16 protected function isValidDefinition(ilADTDefinition $a_def): bool
17 {
18 return $a_def instanceof ilADTMultiEnumDefinition;
19 }
20
21 public function reset(): void
22 {
23 parent::reset();
24 $this->values = null;
25 }
26
27 // properties
28
33 abstract protected function handleSelectionValue($a_value);
34
35 public function addSelection(int $value_index): void
36 {
37 if (!$this->isValidOption($value_index)) {
38 return;
39 }
40 $this->values[] = $value_index;
41 }
42
43 public function setSelections(array $a_values = null): void
44 {
45 if ($a_values === null) {
46 return;
47 }
48 $checked_values = [];
49 foreach ($a_values as $value_index) {
50 $clean_value = $this->handleSelectionValue($value_index);
51 if ($this->isValidOption($clean_value)) {
52 $checked_values[] = (int) $clean_value;
53 }
54 }
55 $this->values = count($checked_values) ? $checked_values : null;
56 }
57
58 public function getSelections(): ?array
59 {
60 return $this->values;
61 }
62
67 public function isValidOption($a_value): bool
68 {
69 $a_value = $this->handleSelectionValue($a_value);
70 return array_key_exists($a_value, $this->getDefinition()->getOptions());
71 }
72
73 // comparison
74
75 public function equals(ilADT $a_adt): ?bool
76 {
77 if ($this->getDefinition()->isComparableTo($a_adt)) {
78 return ($this->getCheckSum() === $a_adt->getCheckSum());
79 }
80 return null;
81 }
82
83 public function isLarger(ilADT $a_adt): ?bool
84 {
85 return null;
86 }
87
88 public function isSmaller(ilADT $a_adt): ?bool
89 {
90 return null;
91 }
92
93 // null
94
95 public function isNull(): bool
96 {
97 return $this->getSelections() === null;
98 }
99
100 public function getCheckSum(): ?string
101 {
102 if (!$this->isNull()) {
103 $current = $this->getSelections();
104 sort($current);
105 return md5(implode(",", $current));
106 }
107 return null;
108 }
109
110 // stdClass
111
112 public function exportStdClass(): ?stdClass
113 {
114 if (!$this->isNull()) {
115 $obj = new stdClass();
116 $obj->value = (array) $this->getSelections();
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->setSelections($a_std->value);
126 }
127 }
128}
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:12
getCheckSum()
Get unique checksum.
getDefinition()
Get definition.
Definition: class.ilADT.php:92