ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
RecordTransformation.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 $key => $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 if (false === is_string($key)) {
42 'The array key MUST be a string',
43 'key_is_not_a_string'
44 );
45 }
46 }
47
48 $this->transformations = $transformations;
49 }
50
54 public function transform($from)
55 {
56 $result = array();
57
58 $this->validateValueLength($from);
59
60 foreach ($from as $key => $value) {
61 if (false === is_string($key)) {
63 'The array key MUST be a string',
64 'key_is_not_a_string'
65 );
66 }
67
68 if (false === isset($this->transformations[$key])) {
70 sprintf('Could not find transformation for array key "%s"', $key),
71 'array_key_does_not_exist',
72 $key
73 );
74 }
75
76 $transformation = $this->transformations[$key];
77 $transformedValue = $transformation->transform($value);
78
79 $result[$key] = $transformedValue;
80 }
81
82 return $result;
83 }
84
88 public function __invoke($from)
89 {
90 return $this->transform($from);
91 }
92
97 private function validateValueLength($values)
98 {
99 $countOfValues = count($values);
100 $countOfTransformations = count($this->transformations);
101
102 if ($countOfValues !== $countOfTransformations) {
104 sprintf(
105 'The given values(count: "%s") does not match with the given transformations("%s")',
106 $countOfValues,
107 $countOfTransformations
108 ),
109 'length_does_not_match',
110 $countOfValues,
111 $countOfTransformations
112 );
113 }
114 }
115}
$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.