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) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
10
15
17{
20
25
29 public function __construct(array $transformations)
30 {
31 foreach ($transformations as $transformation) {
32 if (!$transformation instanceof Transformation) {
33 $transformationClassName = Transformation::class;
34
36 sprintf('The array MUST contain only "%s" instances', $transformationClassName),
37 'not_a_transformation',
38 $transformationClassName
39 );
40 }
41 }
42
43 $this->transformations = $transformations;
44 }
45
49 public function transform($from)
50 {
51 $this->validateValueLength($from);
52
53 $result = array();
54 foreach ($from as $key => $value) {
55 if (false === array_key_exists($key, $this->transformations)) {
57 sprintf(
58 'There is no entry "%s" defined in the transformation array',
59 $key
60 ),
61 'values_do_not_match',
62 $key
63 );
64 }
65 $transformedValue = $this->transformations[$key]->transform($value);
66
67 $result[] = $transformedValue;
68 }
69
70 return $result;
71 }
72
76 private function validateValueLength($values)
77 {
78 $countOfValues = count($values);
79 $countOfTransformations = count($this->transformations);
80
81 if ($countOfValues !== $countOfTransformations) {
83 sprintf(
84 'The given values(count: "%s") does not match with the given transformations("%s")',
85 $countOfValues,
86 $countOfTransformations
87 ),
88 'given_values_',
89 $countOfValues,
90 $countOfTransformations
91 );
92 }
93 }
94}
$result
An exception for terminatinating execution or to throw for unit testing.
transform($from)
Perform the transformation.Please use this for transformations. It's more performant than calling inv...
A transformation is a function from one datatype to another.