[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / ArrayUtility.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Utility;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
19
20 /**
21 * Collection of static array utility functions
22 *
23 * Scope: frontend / backend
24 * @internal
25 */
26 class ArrayUtility
27 {
28
29 /**
30 * Validates the given $arrayToTest by checking if an element is not in $allowedArrayKeys.
31 *
32 * @param array $arrayToTest
33 * @param array $allowedArrayKeys
34 * @return void
35 * @throws TypeDefinitionNotValidException if an element in $arrayToTest is not in $allowedArrayKeys
36 * @internal
37 */
38 public static function assertAllArrayKeysAreValid(array $arrayToTest, array $allowedArrayKeys)
39 {
40 $notAllowedArrayKeys = array_keys(array_diff_key($arrayToTest, array_flip($allowedArrayKeys)));
41 if (count($notAllowedArrayKeys) !== 0) {
42 throw new TypeDefinitionNotValidException(sprintf('The options "%s" were not allowed (allowed were: "%s")', implode(', ', $notAllowedArrayKeys), implode(', ', $allowedArrayKeys)), 1325697085);
43 }
44 }
45
46 /**
47 * Sort keys from the current nesting level if all keys within the
48 * current nesting level are integers.
49 *
50 * @param array $array
51 * @return array
52 * @internal
53 */
54 public static function sortNumericArrayKeysRecursive(array $array): array
55 {
56 if (count(array_filter(array_keys($array), 'is_string')) === 0) {
57 ksort($array);
58 }
59 foreach ($array as $key => $value) {
60 if (is_array($value) && !empty($value)) {
61 $array[$key] = self::sortNumericArrayKeysRecursive($value);
62 }
63 }
64 return $array;
65 }
66
67 /**
68 * Reindex keys from the current nesting level if all keys within
69 * the current nesting level are integers.
70 *
71 * @param array $array
72 * @return array
73 * @internal
74 */
75 public static function reIndexNumericArrayKeysRecursive(array $array): array
76 {
77 if (count(array_filter(array_keys($array), 'is_string')) === 0) {
78 $array = array_values($array);
79 }
80 foreach ($array as $key => $value) {
81 if (is_array($value) && !empty($value)) {
82 $array[$key] = self::reIndexNumericArrayKeysRecursive($value);
83 }
84 }
85 return $array;
86 }
87
88 /**
89 * Recursively remove keys if their value are NULL.
90 *
91 * @param array $array
92 * @return array the modified array
93 * @internal
94 */
95 public static function removeNullValuesRecursive(array $array): array
96 {
97 $result = $array;
98 foreach ($result as $key => $value) {
99 if (is_array($value)) {
100 $result[$key] = self::removeNullValuesRecursive($value);
101 } elseif ($value === null) {
102 unset($result[$key]);
103 }
104 }
105 return $result;
106 }
107
108 /**
109 * Recursively translate values.
110 *
111 * @param array $array
112 * @return array the modified array
113 * @internal
114 */
115 public static function stripTagsFromValuesRecursive(array $array): array
116 {
117 $result = $array;
118 foreach ($result as $key => $value) {
119 if (is_array($value)) {
120 $result[$key] = self::stripTagsFromValuesRecursive($value);
121 } else {
122 if (!is_bool($value)) {
123 $result[$key] = strip_tags($value);
124 }
125 }
126 }
127 return $result;
128 }
129
130 /**
131 * Recursively convert 'true' and 'false' strings to boolen values.
132 *
133 * @param array $array
134 * @return array the modified array
135 * @internal
136 */
137 public static function convertBooleanStringsToBooleanRecursive(array $array): array
138 {
139 $result = $array;
140 foreach ($result as $key => $value) {
141 if (is_array($value)) {
142 $result[$key] = self::convertBooleanStringsToBooleanRecursive($value);
143 } else {
144 if ($value === 'true') {
145 $result[$key] = true;
146 } elseif ($value === 'false') {
147 $result[$key] = false;
148 }
149 }
150 }
151 return $result;
152 }
153 }