ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
14
16{
18
23
27 public function __construct(array $transformations)
28 {
29 foreach ($transformations as $transformation) {
30 if (!$transformation instanceof Transformation) {
31 $transformationClassName = Transformation::class;
32
34 sprintf('The array MUST contain only "%s" instances', $transformationClassName),
35 'not_a_transformation',
36 $transformationClassName
37 );
38 }
39 }
40
41 $this->transformations = $transformations;
42 }
43
47 public function transform($from)
48 {
49 $this->validateValueLength($from);
50
51 $result = array();
52 foreach ($from as $key => $value) {
53 if (false === array_key_exists($key, $this->transformations)) {
55 sprintf(
56 'There is no entry "%s" defined in the transformation array',
57 $key
58 ),
59 'values_do_not_match',
60 $key
61 );
62 }
63 $transformedValue = $this->transformations[$key]->transform($value);
64
65 $result[] = $transformedValue;
66 }
67
68 return $result;
69 }
70
74 public function __invoke($from)
75 {
76 return $this->transform($from);
77 }
78
82 private function validateValueLength($values)
83 {
84 $countOfValues = count($values);
85 $countOfTransformations = count($this->transformations);
86
87 if ($countOfValues !== $countOfTransformations) {
89 sprintf(
90 'The given values(count: "%s") does not match with the given transformations("%s")',
91 $countOfValues,
92 $countOfTransformations
93 ),
94 'given_values_',
95 $countOfValues,
96 $countOfTransformations
97 );
98 }
99 }
100}
$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...
__invoke($from)
Transformations should be callable.This MUST do the same as transform.InvalidArgumentException if the...
A transformation is a function from one datatype to another.