ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Dataset.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
29{
30 protected array $dimensions = [];
31 protected array $dimension_groups = [];
32 protected array $points = [];
33 protected array $alternative_information = [];
34
39 public function __construct(array $dimensions, array $dimension_groups = [])
40 {
41 foreach ($dimensions as $name => $dimension) {
42 if (!is_string($name)) {
43 throw new \InvalidArgumentException(
44 "Expected array key to be a string, '$name' is given."
45 );
46 }
47 if (!$dimension instanceof Dimension\Dimension) {
48 throw new \InvalidArgumentException(
49 "Expected array value to be an instance of Dimension." .
50 (is_object($dimension) ? get_class($dimension) : gettype($dimension)) . " is given."
51 );
52 }
53 }
54 $this->dimensions = $dimensions;
55
56 foreach ($dimension_groups as $group_name => $group) {
57 if (!is_string($group_name)) {
58 throw new \InvalidArgumentException(
59 "Expected array key to be a string, '$group_name' is given."
60 );
61 }
62 if (!$group instanceof Dimension\DimensionGroup) {
63 throw new \InvalidArgumentException(
64 "Expected array value to be an instance of DimensionGroup. " .
65 (is_object($group) ? get_class($group) : gettype($group)) . " is given."
66 );
67 }
68 }
69 $this->dimension_groups = $dimension_groups;
70 }
71
75 public function getDimensions(): array
76 {
77 return $this->dimensions;
78 }
79
83 public function getDimensionGroups(): array
84 {
86 }
87
88 protected function checkDimensionCongruenceForValues(array $values): void
89 {
90 if (array_diff_key($values, $this->getDimensions())
91 || array_diff_key($this->getDimensions(), $values)
92 ) {
93 throw new \ArgumentCountError(
94 "The number of the passed values does not match with the number of Dimensions."
95 );
96 }
97 }
98
104 public function withPoint(string $measurement_item_label, array $values): self
105 {
106 $this->checkDimensionCongruenceForValues($values);
107
108 foreach ($values as $dimension_name => $value) {
109 $dimension = $this->getDimensions()[$dimension_name];
110 $dimension->checkValue($value);
111 }
112
113 $clone = clone $this;
114 $clone->points[$measurement_item_label] = $values;
115
116 return $clone;
117 }
118
119 public function getPoints(): array
120 {
121 return $this->points;
122 }
123
124 public function getPointsPerDimension(): array
125 {
126 $points_restructured = [];
127 foreach ($this->getPoints() as $measurement_item_label => $points_for_dimensions) {
128 foreach ($points_for_dimensions as $dimension_name => $point) {
129 $points_restructured[$dimension_name][$measurement_item_label] = $point;
130 }
131 }
132
133 return $points_restructured;
134 }
135
141 public function withAlternativeInformation(string $measurement_item_label, array $values): self
142 {
143 $this->checkDimensionCongruenceForValues($values);
144
145 foreach ($values as $dimension_name => $value) {
146 if (!is_string($value) && !is_null($value)) {
147 throw new \InvalidArgumentException(
148 "Expected array value to be a string or null, '$value' is given."
149 );
150 }
151 }
152
153 $clone = clone $this;
154 $clone->alternative_information[$measurement_item_label] = $values;
155
156 return $clone;
157 }
158
159 public function getAlternativeInformation(): array
160 {
162 }
163
165 {
166 $alternative_information_restructured = [];
167 foreach ($this->getAlternativeInformation() as $measurement_item_label => $alternative_information_for_dimensions) {
168 foreach ($alternative_information_for_dimensions as $dimension_name => $alternative_info) {
169 $alternative_information_restructured[$dimension_name][$measurement_item_label] = $alternative_info;
170 }
171 }
172
173 return $alternative_information_restructured;
174 }
175
179 public function withResetDataset(): self
180 {
181 $clone = clone $this;
182 $clone->points = [];
183 $clone->alternative_information = [];
184 return $clone;
185 }
186
187 public function isEmpty(): bool
188 {
189 if (empty($this->getPoints())) {
190 return true;
191 }
192 return false;
193 }
194
195 public function getMinValueForDimension(string $dimension_name): float
196 {
197 if ($this->isEmpty()) {
198 throw new \RuntimeException(
199 "Dataset must not be empty."
200 );
201 }
202
203 $min = null;
204 $dimension_points = $this->getPointsPerDimension()[$dimension_name];
205 foreach ($dimension_points as $point) {
206 if (is_array($point)) {
207 foreach ($point as $p) {
208 if (is_null($min) || $p < $min) {
209 $min = $p;
210 }
211 }
212 } elseif (is_null($min) || !is_null($point) && $point < $min) {
213 $min = $point;
214 }
215 }
216
217 return (float) $min;
218 }
219
220 public function getMaxValueForDimension(string $dimension_name): float
221 {
222 if ($this->isEmpty()) {
223 throw new \RuntimeException(
224 "Dataset must not be empty."
225 );
226 }
227
228 $max = null;
229 $dimension_points = $this->getPointsPerDimension()[$dimension_name];
230 foreach ($dimension_points as $point) {
231 if (is_array($point)) {
232 foreach ($point as $p) {
233 if (is_null($max) || $p > $max) {
234 $max = $p;
235 }
236 }
237 } elseif (is_null($max) || !is_null($point) && $point > $max) {
238 $max = $point;
239 }
240 }
241
242 return (float) $max;
243 }
244}
__construct(array $dimensions, array $dimension_groups=[])
Definition: Dataset.php:39
withAlternativeInformation(string $measurement_item_label, array $values)
Definition: Dataset.php:141
withPoint(string $measurement_item_label, array $values)
Definition: Dataset.php:104
getMinValueForDimension(string $dimension_name)
Definition: Dataset.php:195
array $alternative_information
Definition: Dataset.php:33
checkDimensionCongruenceForValues(array $values)
Definition: Dataset.php:88
withResetDataset()
Returns an empty Dataset clone.
Definition: Dataset.php:179
getMaxValueForDimension(string $dimension_name)
Definition: Dataset.php:220