‪TYPO3CMS  ‪main
PermutationUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
24 {
36  public static function ‪meltStringItems(array $payload, string $previousResult = ''): array
37  {
38  $results = [];
39  $items = static::nextItems($payload);
40  foreach ($items as $item) {
41  if (!(is_string($item) || $item instanceof \Stringable)) {
42  throw new \LogicException(
43  sprintf('Expected string, got %s', gettype($item)),
44  1578164102
45  );
46  }
47  $resultItem = $previousResult . $item;
48  if (!empty($payload)) {
49  $results = array_merge(
50  $results,
51  static::meltStringItems($payload, $resultItem)
52  );
53  continue;
54  }
55  $results[] = $resultItem;
56  }
57  return $results;
58  }
59 
72  public static function ‪meltArrayItems(array $payload, array $previousResult = []): array
73  {
74  $results = [];
75  $items = static::nextItems($payload);
76  foreach ($items as $item) {
77  $resultItems = $previousResult;
78  $resultItems[] = $item;
79  if (!empty($payload)) {
80  $results = array_merge(
81  $results,
82  static::meltArrayItems($payload, $resultItems)
83  );
84  continue;
85  }
86  $results[] = $resultItems;
87  }
88  return $results;
89  }
90 
91  protected static function ‪nextItems(array &$payload): iterable
92  {
93  $items = array_shift($payload);
94  if (is_iterable($items)) {
95  return $items;
96  }
97  throw new \LogicException(
98  sprintf('Expected iterable, got %s', gettype($items)),
99  1578164101
100  );
101  }
102 }
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\PermutationUtility\nextItems
‪static nextItems(array &$payload)
Definition: PermutationUtility.php:91
‪TYPO3\CMS\Core\Utility\PermutationUtility
Definition: PermutationUtility.php:24
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltArrayItems
‪static array[] meltArrayItems(array $payload, array $previousResult=[])
Definition: PermutationUtility.php:72
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltStringItems
‪static string[] meltStringItems(array $payload, string $previousResult='')
Definition: PermutationUtility.php:36