ILIAS  release_7 Revision v7.30-3-g800a261c036
TupleTransformation.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3/* Copyright (c) 2020 Luka K. A. Stocker, Extended GPL, see docs/LICENSE */
4
6
11
13{
16
18
22 public function __construct(array $transformations)
23 {
24 foreach ($transformations as $transformation) {
25 if (!$transformation instanceof Transformation) {
26 $transformationClassName = Transformation::class;
27
29 sprintf('The array must contain only "%s" instances', $transformationClassName),
30 'not_a_transformation',
31 $transformationClassName
32 );
33 }
34 }
35 $this->transformations = $transformations;
36 }
37
41 public function transform($from)
42 {
43 if (!is_array($from)) {
44 $from = [$from];
45 }
46
47 if ([] === $from) {
49 sprintf('The array "%s" ist empty', $from),
50 'value_array_is_empty',
51 $from
52 ) ;
53 }
54
55 $this->testLengthOf($from);
56
57 $result = [];
58 foreach ($from as $key => $value) {
59 if (!array_key_exists($key, $this->transformations)) {
61 sprintf('Matching value "%s" not found', $value),
62 'matching_values_not_found',
63 $value
64 );
65 }
66 $transformedValue = $this->transformations[$key]->transform($value);
67 $result[] = $transformedValue;
68 }
69 return $result;
70 }
71
72 private function testLengthOf(array $values) : void
73 {
74 $countOfValues = count($values);
75 $countOfTransformations = count($this->transformations);
76
77 if ($countOfValues !== $countOfTransformations) {
79 sprintf('The length of given value "%s" does not match with the given transformations', $countOfValues),
80 'given_values_',
81 $countOfValues
82 );
83 }
84 }
85}
$result
An exception for terminatinating execution or to throw for unit testing.
A transformation is a function from one datatype to another.