ILIAS  release_8 Revision v8.24
Dataset.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
23
25
30{
31 protected array $dimensions = [];
32 protected array $points = [];
33 protected array $alternative_information = [];
34
38 public function __construct(array $dimensions)
39 {
40 foreach ($dimensions as $name => $dimension) {
41 if (!is_string($name)) {
42 throw new \InvalidArgumentException(
43 "Expected array key to be a string, '$name' is given."
44 );
45 }
46 if (!$dimension instanceof Dimension\Dimension) {
47 throw new \InvalidArgumentException(
48 "Expected array value to be an instance of Dimension, '$dimension' is given."
49 );
50 }
51 }
52 $this->dimensions = $dimensions;
53 }
54
55 public function getDimensions(): array
56 {
57 return $this->dimensions;
58 }
59
60 protected function checkDimensionCongruenceForValues(array $values): void
61 {
62 if (array_diff_key($values, $this->getDimensions())
63 || array_diff_key($this->getDimensions(), $values)
64 ) {
65 throw new \ArgumentCountError(
66 "The number of the passed values does not match with the number of Dimensions."
67 );
68 }
69 }
70
76 public function withPoint(string $measurement_item_label, array $values): self
77 {
79
80 foreach ($values as $dimension_name => $value) {
81 $dimension = $this->getDimensions()[$dimension_name];
82 $dimension->checkValue($value);
83 }
84
85 $clone = clone $this;
86 $clone->points[$measurement_item_label] = $values;
87
88 return $clone;
89 }
90
91 public function getPoints(): array
92 {
93 return $this->points;
94 }
95
96 public function getPointsPerDimension(): array
97 {
98 $points_restructured = [];
99 foreach ($this->getPoints() as $measurement_item_label => $points_for_dimensions) {
100 foreach ($points_for_dimensions as $dimension_name => $point) {
101 $points_restructured[$dimension_name][$measurement_item_label] = $point;
102 }
103 }
104
105 return $points_restructured;
106 }
107
113 public function withAlternativeInformation(string $measurement_item_label, array $values): self
114 {
115 $this->checkDimensionCongruenceForValues($values);
116
117 foreach ($values as $dimension_name => $value) {
118 if (!is_string($value) && !is_null($value)) {
119 throw new \InvalidArgumentException(
120 "Expected array value to be a string or null, '$value' is given."
121 );
122 }
123 }
124
125 $clone = clone $this;
126 $clone->alternative_information[$measurement_item_label] = $values;
127
128 return $clone;
129 }
130
131 public function getAlternativeInformation(): array
132 {
134 }
135
137 {
138 $alternative_information_restructured = [];
139 foreach ($this->getAlternativeInformation() as $measurement_item_label => $alternative_information_for_dimensions) {
140 foreach ($alternative_information_for_dimensions as $dimension_name => $alternative_info) {
141 $alternative_information_restructured[$dimension_name][$measurement_item_label] = $alternative_info;
142 }
143 }
144
145 return $alternative_information_restructured;
146 }
147
151 public function withResetDataset(): self
152 {
153 $clone = clone $this;
154 $clone->points = [];
155 $clone->alternative_information = [];
156 return $clone;
157 }
158
159 public function isEmpty(): bool
160 {
161 if (empty($this->getPoints())) {
162 return true;
163 }
164 return false;
165 }
166
167 public function getMinValueForDimension(string $dimension_name): float
168 {
169 if ($this->isEmpty()) {
170 throw new \RuntimeException(
171 "Dataset must not be empty."
172 );
173 }
174
175 $min = null;
176 $dimension_points = $this->getPointsPerDimension()[$dimension_name];
177 foreach ($dimension_points as $point) {
178 if (is_array($point)) {
179 foreach ($point as $p) {
180 if (is_null($min) || $p < $min) {
181 $min = $p;
182 }
183 }
184 } elseif (is_null($min) || !is_null($point) && $point < $min) {
185 $min = $point;
186 }
187 }
188
189 return (float) $min;
190 }
191
192 public function getMaxValueForDimension(string $dimension_name): float
193 {
194 if ($this->isEmpty()) {
195 throw new \RuntimeException(
196 "Dataset must not be empty."
197 );
198 }
199
200 $max = null;
201 $dimension_points = $this->getPointsPerDimension()[$dimension_name];
202 foreach ($dimension_points as $point) {
203 if (is_array($point)) {
204 foreach ($point as $p) {
205 if (is_null($max) || $p > $max) {
206 $max = $p;
207 }
208 }
209 } elseif (is_null($max) || !is_null($point) && $point > $max) {
210 $max = $point;
211 }
212 }
213
214 return (float) $max;
215 }
216}
withAlternativeInformation(string $measurement_item_label, array $values)
Definition: Dataset.php:113
withPoint(string $measurement_item_label, array $values)
Definition: Dataset.php:76
getMinValueForDimension(string $dimension_name)
Definition: Dataset.php:167
array $alternative_information
Definition: Dataset.php:33
checkDimensionCongruenceForValues(array $values)
Definition: Dataset.php:60
withResetDataset()
Returns an empty Dataset clone.
Definition: Dataset.php:151
__construct(array $dimensions)
Definition: Dataset.php:38
getMaxValueForDimension(string $dimension_name)
Definition: Dataset.php:192
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Dataset.php:22
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...