ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ListValue.php
Go to the documentation of this file.
1<?php
2
4
7use ILIAS\BackgroundTasks\Implementation\Values\PrimitiveValueWrapperFactory;
8use ILIAS\BackgroundTasks\Implementation\Values\ScalarValues\BasicScalarValueFactory;
12use ILIAS\BackgroundTasks\ValueType;
13
25{
26 use BasicScalarValueFactory;
30 protected $list = array();
34 protected $type;
35
36
42 public function __construct()
43 {
44 }
45
46
47 protected function deriveType($list)
48 {
49 return new ListType($this->calculateLowestCommonType($this->getTypes($list)));
50 }
51
52
62 protected function calculateLowestCommonType($types)
63 {
64 // If the list is empty the type should be [] (empty list).
65 if (!count($types)) {
66 return null;
67 }
68
69 if (count($types) == 1) {
70 return $types[0];
71 }
72
73 $ancestorsList = [];
74 foreach ($types as $object) {
75 if (!$object instanceof Type) {
76 throw new InvalidArgumentException("List Type must be constructed with instances of Type.");
77 }
78 $ancestorsList[] = $object->getAncestors();
79 }
80
81 $lct = $ancestorsList[0][0];
82 foreach ($ancestorsList[0] as $i => $ancestors) {
83 if ($this->sameClassOnLevel($ancestorsList, $i)) {
84 $lct = $ancestors;
85 } else {
86 return $lct;
87 }
88 }
89
90 // We reach this point if the types are equal.
91 return $lct;
92 }
93
94
101 protected function sameClassOnLevel($ancestorsList, $i)
102 {
103 $class = $ancestorsList[0][$i];
104 foreach ($ancestorsList as $class_hierarchy) {
105 if (count($class_hierarchy) <= $i) {
106 return false;
107 }
108 if (!$class_hierarchy[$i]->equals($class)) {
109 return false;
110 }
111 }
112
113 return true;
114 }
115
116
117 protected function getTypes($list)
118 {
119 $types = [];
120 foreach ($list as $value) {
121 $valueWrapped = $this->wrapValue($value);
122 $this->list[] = $valueWrapped;
123 $types[] = $valueWrapped->getType();
124 }
125
126 return $types;
127 }
128
129
137 public function serialize()
138 {
139 return serialize($this->list);
140 }
141
142
155 public function unserialize($serialized)
156 {
157 $this->list = unserialize($serialized);
158 $this->type = $this->deriveType($this->list);
159 }
160
161
167 public function getHash()
168 {
169 $hashes = '';
170 foreach ($this->getList() as $value) {
171 $hashes .= $value->getHash();
172 }
173
174 return md5($hashes);
175 }
176
177
183 public function equals(Value $other)
184 {
185 if (!$other instanceof ListValue) {
186 return false;
187 }
188
189 if ($this->getType() != $other->getType()) {
190 return false;
191 }
192
193 $values = $this->getList();
194 $otherValues = $other->getList();
195
196 if (count($values) != count($otherValues)) {
197 return false;
198 }
199
200 for ($i = 0; $i < count($values); $i++) {
201 if (!$values[$i]->equals($otherValues[$i])) {
202 ;
203 }
204 }
205
206 return true;
207 }
208
209
213 public function getList()
214 {
215 return $this->list;
216 }
217
218
225 protected function getClassHierarchy($object)
226 {
227 if (!is_object($object)) {
228 throw new InvalidArgumentException("Given Value $object must be an object.");
229 }
230
231 $hierarchy = [];
232 $class = get_class($object);
233
234 do {
235 $hierarchy[] = $class;
236 } while (($class = get_parent_class($class)) !== false);
237
238 return $hierarchy;
239 }
240
241
245 public function getType()
246 {
247 return $this->type;
248 }
249
250
255 public function setValue($list)
256 {
257 $this->type = $this->deriveType($list);
258 }
259}
An exception for terminatinating execution or to throw for unit testing.
calculateLowestCommonType($types)
Todo: This implementation is not performing well (needs the most iterations) on lists with all the sa...
Definition: ListValue.php:62
$i
Definition: disco.tpl.php:19
$values