ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ListValue.php
Go to the documentation of this file.
1<?php
2
20
23use ILIAS\BackgroundTasks\Implementation\Values\ScalarValues\BasicScalarValueFactory;
27
36{
37 use BasicScalarValueFactory;
38
42 protected array $list = [];
43 protected Type $type;
44
45 protected function deriveType(array $list): ListType
46 {
47 $type = $this->calculateLowestCommonType($this->getTypes($list));
48
49 return new ListType($type);
50 }
51
59 protected function calculateLowestCommonType(array $types)
60 {
61 // If the list is empty the type should be [] (empty list).
62 if ($types === []) {
63 return null;
64 }
65
66 if (count($types) == 1) {
67 return $types[0];
68 }
69
70 $ancestorsList = [];
71 foreach ($types as $object) {
72 if (!$object instanceof Type) {
73 throw new InvalidArgumentException("List Type must be constructed with instances of Type.");
74 }
75 $ancestorsList[] = $object->getAncestors();
76 }
77
78 $lct = $ancestorsList[0][0];
79 foreach ($ancestorsList[0] as $i => $ancestors) {
80 if ($this->sameClassOnLevel($ancestorsList, $i)) {
81 $lct = $ancestors;
82 } else {
83 return $lct;
84 }
85 }
86
87 // We reach this point if the types are equal.
88 return $lct;
89 }
90
91 protected function sameClassOnLevel(array $ancestorsList, int $i): bool
92 {
93 $class = $ancestorsList[0][$i];
94 foreach ($ancestorsList as $class_hierarchy) {
95 if (count($class_hierarchy) <= $i) {
96 return false;
97 }
98 if (!$class_hierarchy[$i]->equals($class)) {
99 return false;
100 }
101 }
102
103 return true;
104 }
105
109 protected function getTypes(array $list): array
110 {
111 $types = [];
112 foreach ($list as $value) {
113 $valueWrapped = $this->wrapValue($value);
114 $this->list[] = $valueWrapped;
115 $types[] = $valueWrapped->getType();
116 }
117
118 return $types;
119 }
120
127 public function serialize()
128 {
129 return serialize($this->list);
130 }
131
141 public function unserialize($serialized): void
142 {
143 $this->list = unserialize($serialized);
144 $this->type = $this->deriveType($this->list);
145 }
146
152 public function getHash(): string
153 {
154 $hashes = '';
155 foreach ($this->getList() as $value) {
156 $hashes .= $value->getHash();
157 }
158
159 return md5($hashes);
160 }
161
162 public function equals(Value $other): bool
163 {
164 if (!$other instanceof ListValue) {
165 return false;
166 }
167
168 if ($this->getType() != $other->getType()) {
169 return false;
170 }
171
172 $values = $this->getList();
173 $otherValues = $other->getList();
174
175 if (count($values) !== count($otherValues)) {
176 return false;
177 }
178
179 foreach ($values as $i => $value) {
180 if (!$value->equals($otherValues[$i])) {
181 ;
182 }
183 }
184
185 return true;
186 }
187
191 public function getList(): array
192 {
193 return $this->list;
194 }
195
201 protected function getClassHierarchy($object): array
202 {
203 if (!is_object($object)) {
204 throw new InvalidArgumentException("Given Value $object must be an object.");
205 }
206
207 $hierarchy = [];
208 $class = $object::class;
209
210 do {
211 $hierarchy[] = $class;
212 } while (($class = get_parent_class($class)) !== false);
213
214 return $hierarchy;
215 }
216
217 #[\Override]
218 public function getType(): Type
219 {
220 return $this->type;
221 }
222
226 public function setValue($value): void
227 {
228 $this->type = $this->deriveType($value);
229 }
230}
Class Exception Thrown if some procedure did not get the arguments it expected.
unserialize($serialized)
Constructs the object http://php.net/manual/en/serializable.unserialize.php.
Definition: ListValue.php:141
serialize()
String representation of object http://php.net/manual/en/serializable.serialize.php.
Definition: ListValue.php:127
calculateLowestCommonType(array $types)
Todo: This implementation is not performing well (needs the most iterations) on lists with all the sa...
Definition: ListValue.php:59
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ListValue.php:19